home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / test < prev    next >
Text File  |  1995-11-18  |  73KB  |  3,194 lines

  1. //
  2. // Test Suite for RLaB.
  3. // This test suite is supposed to test as much as possible, and still
  4. // be runable on most platforms. This means we cannot do graphics or
  5. // other platform specific stuff like piping. However, that is OK,
  6. // since we are mostly interested in assuring the builder that RLaB
  7. // will produce correct numerical answers.
  8.  
  9. // We use randsvd, which in turn uses RLaB's random number
  10. // generator. We try to be carefull and seed the random number
  11. // generator so that each user will get similar results (or at least
  12. // similar inputs to the tests).
  13.  
  14. // Test the other style comments 
  15. 1 + 2; // A simple statement with trailing comment.
  16. #  Optional RLaB comment style.
  17. 1 + 2; #  A simple statement with trailing comment.
  18. %  Optional Matlab comment style.
  19. 1 + 2; %  A simple statement with trailing comment.
  20.  
  21. srand (SEED = 10);   // Seems to produce reasonable results
  22. rand("default");
  23.  
  24. tic();    // Start timing the tests...
  25.  
  26. //
  27. // Test Parameters and some functions we will need later
  28. //
  29.  
  30. pi = 4.0*atan(1.0);
  31. X = 3;    // should be 3 (heuristic).
  32.  
  33. //
  34. // Compute machine epsilon
  35. //
  36.  
  37. epsilon = function() 
  38. {
  39.   eps = 1.0;
  40.   while((eps + 1.0) != 1.0) 
  41.   {
  42.     eps = eps/2.0;
  43.   }
  44.   return 2*eps;
  45. };
  46.  
  47. eps = epsilon();
  48.  
  49. eye = function( m , n ) 
  50. {
  51.   if (!exist (n))
  52.   {
  53.     if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
  54.     new = zeros (m[1], m[2]);
  55.     N = min ([m[1], m[2]]);
  56.   else
  57.     if (class (m) == "string" || class (n) == "string") {
  58.       error ("eye(), string arguments not allowed");
  59.     }
  60.     if (max (size (m)) == 1 && max (size (n)) == 1)
  61.     {
  62.       new = zeros (m[1], n[1]);
  63.       N = min ([m[1], n[1]]);
  64.     else
  65.       error ("matrix arguments to eye() must be 1x1");
  66.     }
  67.   }
  68.   for(i in 1:N)
  69.   {
  70.     new[i;i] = 1.0;
  71.   }
  72.   return new;
  73. };
  74.  
  75. symm = function( A )
  76. {
  77.   return (A + A')./2;
  78. };
  79.  
  80. //-------------------------------------------------------------------//
  81.  
  82. // Synopsis:    Pascal matrix.
  83.  
  84. // Syntax:    P = pascal ( N )
  85.  
  86. // Description:
  87.  
  88. //    The Pascal matrix of order N: a symmetric positive definite
  89. //    matrix with integer entries taken from Pascal's triangle. The
  90. //    Pascal matrix is totally positive and its inverse has integer
  91. //    entries.  Its eigenvalues occur in reciprocal pairs. COND(P)
  92. //    is approximately 16^N/(N*PI) for large N. PASCAL(N,1) is the
  93. //    lower triangular Cholesky factor (up to signs of columns) of
  94. //    the Pascal matrix.   It is involutary (is its own
  95. //    inverse). PASCAL(N,2) is a transposed and permuted version of
  96. //    PASCAL(N,1) which is a cube root of the identity.
  97.  
  98. //      References:
  99. //      R. Brawer and M. Pirovino, The linear algebra of the Pascal matrix,
  100. //           Linear Algebra and Appl., 174 (1992), pp. 13-23 (this paper
  101. //           gives a factorization of L = PASCAL(N,1) and a formula for the
  102. //           elements of L^k).
  103. //      S. Karlin, Total Positivity, Volume 1, Stanford University Press,
  104. //           1968.  (Page 137: shows i+j-1 choose j is TP (i,j=0,1,...).
  105. //                   PASCAL(N) is a submatrix of this matrix.)
  106. //      M. Newman and J. Todd, The evaluation of matrix inversion programs,
  107. //           J. Soc. Indust. Appl. Math., 6(4):466--476, 1958.
  108. //      H. Rutishauser, On test matrices, Programmation en Mathematiques
  109. //           Numeriques, Editions Centre Nat. Recherche Sci., Paris, 165,
  110. //           1966, pp. 349-365.  (Gives an integral formula for the
  111. //           elements of PASCAL(N).)
  112. //      J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
  113. //           Birkhauser, Basel, and Academic Press, New York, 1977, p. 172.
  114. //      H.W. Turnbull, The Theory of Determinants, Matrices, and Invariants,
  115. //           Blackie, London and Glasgow, 1929.  (PASCAL(N,2) on page 332.)
  116.  
  117. //    This file is a translation of pascal.m from version 2.0 of
  118. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  119. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  120.  
  121. //-------------------------------------------------------------------//
  122.  
  123. pascal = function ( n , k )
  124. {
  125.   local(n, k)
  126.  
  127.   if (!exist (k)) { k = 0; }
  128.  
  129.   P = diag( (-1).^[0:n-1] );
  130.   P[; 1] = ones(n,1);
  131.  
  132.   //  Generate the Pascal Cholesky factor (up to signs).
  133.  
  134.   for (j in 2:n-1)
  135.   {
  136.     for (i in j+1:n)
  137.     {
  138.       P[i;j] = P[i-1;j] - P[i-1;j-1];
  139.     }
  140.   }
  141.  
  142.   if (k == 0)
  143.   {
  144.     P = P*P';
  145.   else if (k == 2) {
  146.     P = rot90(P,3);
  147.     if (n/2 == round(n/2)) { P = -P; }
  148.   }}
  149.  
  150.   return P;
  151. };
  152.  
  153. //-------------------------------------------------------------------//
  154.  
  155. // Synopsis:    Random, orthogonal upper Hessenberg matrix.
  156.  
  157. // Syntax:      H = ohess ( N )
  158.  
  159. // Description:
  160.  
  161. //      H is an N-by-N real, random, orthogonal upper Hessenberg
  162. //      matrix. Alternatively, H = OHESS(X), where X is an arbitrary
  163. //      real N-vector (N > 1) constructs H non-randomly using the
  164. //      elements of X as parameters. In both cases H is constructed
  165. //      via a product of N-1 Givens rotations. 
  166.  
  167. //      Note: See Gragg (1986) for how to represent an N-by-N
  168. //      (complex) unitary Hessenberg matrix with positive subdiagonal
  169. //      elements in terms of 2N-1 real parameters (the Schur
  170. //      parametrization). This M-file handles the real case only and
  171. //      is intended simply as a convenient way to generate random or
  172. //      non-random orthogonal Hessenberg matrices.
  173.  
  174. //      Reference:
  175. //      W.B. Gragg, The QR algorithm for unitary Hessenberg matrices,
  176. //      J. Comp. Appl. Math., 16 (1986), pp. 1-8.
  177.  
  178. //    This file is a translation of ohess.m from version 2.0 of
  179. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  180. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  181.  
  182. //-------------------------------------------------------------------//
  183.  
  184. ohess = function ( x )
  185. {
  186.   local (x)
  187.   global (pi)
  188.  
  189.   if (any (imag (x))) { error("Parameter must be real."); }
  190.  
  191.   n = max(size(x));
  192.  
  193.   if (n == 1)
  194.   {
  195.     //  Handle scalar x.
  196.     n = x;
  197.     x = rand(n-1, 1)*2*pi;
  198.     H = eye(n,n);
  199.     H[n;n] = sign(rand());
  200.   else
  201.     H = eye(n,n);
  202.     H[n;n] = sign(x[n]) + (x[n]==0);   // Second term ensures H[n;n] nonzero.
  203.   }
  204.  
  205.   for (i in n:2:-1)
  206.   {
  207.     // Apply Givens rotation through angle x(i-1).
  208.     theta = x[i-1];
  209.     c = cos(theta);
  210.     s = sin(theta);
  211.     H[ [i-1, i] ;] = [ c*H[i-1;]+s*H[i;] ;
  212.                        -s*H[i-1;]+c*H[i;] ];
  213.   }
  214.  
  215.   return H;
  216. };
  217.  
  218. //-------------------------------------------------------------------//
  219.  
  220. // Synopsis:    Random matrix with pre-assigned singular values.
  221.  
  222. // Syntax:    R = randsvd (N, KAPPA, MODE, KL, KU)
  223.  
  224. // Description:
  225.  
  226. //    R is a (banded) random matrix of order N with COND(A) = KAPPA
  227. //    and singular values from the distribution MODE.
  228.  
  229. //      N may be a 2-vector, in which case the matrix is N(1)-by-N(2).
  230. //      Available types:
  231. //             MODE = 1:   one large singular value,
  232. //             MODE = 2:   one small singular value,
  233. //             MODE = 3:   geometrically distributed singular values,
  234. //             MODE = 4:   arithmetically distributed singular values,
  235. //             MODE = 5:   random singular values with unif. dist. logarithm.
  236.  
  237. //      If omitted, MODE defaults to 3, and KAPPA defaults to SQRT(1/EPS).
  238. //      If MODE < 0 then the effect is as for ABS(MODE) except that in the
  239. //      original matrix of singular values the order of the diagonal entries
  240. //      is reversed: small to large instead of large to small.
  241. //      KL and KU are the lower and upper bandwidths respectively; if they
  242. //      are omitted a full matrix is produced.
  243. //      If only KL is present, KU defaults to KL.
  244. //      Special case: if KAPPA < 0 then a random full symmetric positive
  245. //                    definite matrix is produced with COND(A) = -KAPPA and
  246. //                    eigenvalues distributed according to MODE.
  247. //                    KL and KU, if present, are ignored.
  248.  
  249. //      This routine is similar to the more comprehensive Fortran routine xLATMS
  250. //      in the following reference:
  251. //      J.W. Demmel and A. McKenney, A test matrix generation suite,
  252. //      LAPACK Working Note #9, Courant Institute of Mathematical Sciences,
  253. //      New York, 1989.
  254.  
  255. //    This file is a translation of randsvd.m from version 2.0 of
  256. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  257. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  258.  
  259. // Dependencies
  260. #   rfile bandred
  261. #   rfile qmult
  262.  
  263. //-------------------------------------------------------------------//
  264.  
  265. randsvd = function (n, kappa, mode, kl, ku)
  266. {
  267.   local (n, kappa, mode, kl, ku)
  268.  
  269.   if (!exist (kappa)) { kappa = sqrt(1/eps); }
  270.   if (!exist (mode))  { mode = 3; }
  271.   if (!exist (kl))    { kl = n-1; }    // Full matrix.
  272.   if (!exist (ku))    { ku = kl; }     // Same upper and lower bandwidths.
  273.  
  274.   if (abs(kappa) < 1) {
  275.     error("Condition number must be at least 1!");
  276.   }
  277.  
  278.   posdef = 0;
  279.   if (kappa < 0)            // Special case.
  280.   {
  281.     posdef = 1;
  282.     kappa = -kappa;
  283.   }
  284.  
  285.   p = min(n);
  286.   m = n[1];        // Parameter n specifies dimension: m-by-n.
  287.   n = n[max(size(n))];
  288.  
  289.   if (p == 1)        // Handle case where A is a vector.
  290.   {
  291.     rand("normal", -10, 10);
  292.     A = rand(m, n);
  293.     A = A/norm(A, "2");
  294.     return A;
  295.   }
  296.  
  297.   j = abs(mode);
  298.  
  299.   // Set up vector sigma of singular values.
  300.   if (j == 3)
  301.   {
  302.     factor = kappa^(-1/(p-1));
  303.     sigma = factor.^[0:p-1];
  304.  
  305.   else if (j == 4) {
  306.     sigma = ones(p,1) - (0:p-1)'/(p-1)*(1-1/kappa);
  307.  
  308.   else if (j == 5) {    // In this case cond(A) <= kappa.
  309.     rand("uniform", 0, 1)
  310.     sigma = exp( -rand(p,1)*log(kappa) );
  311.  
  312.   else if (j == 2) {
  313.     sigma = ones(p,1);
  314.     sigma[p] = 1/kappa;
  315.  
  316.   else if (j == 1) {
  317.     sigma = ones(p,1)./kappa;
  318.     sigma[1] = 1;
  319.  
  320.   }}}}}
  321.  
  322.  
  323.   // Convert to diagonal matrix of singular values.
  324.   if (mode < 0) {
  325.     sigma = sigma[p:1:-1];
  326.   }
  327.  
  328.   sigma = diag(sigma);
  329.  
  330.   if (posdef)        // Handle special case.
  331.   {
  332.     Q = qmult(p);
  333.     A = Q'*sigma*Q;
  334.     A = (A + A')/2;    // Ensure matrix is symmetric.
  335.     return A;
  336.   }
  337.  
  338.   if (m != n) 
  339.   {
  340.     sigma[m; n] = 0;    // Expand to m-by-n diagonal matrix.
  341.   }
  342.  
  343.   if (kl == 0 && ku == 0) // Diagonal matrix requested - nothing more to do.
  344.   {
  345.     A = sigma;
  346.     return A;
  347.   }
  348.  
  349.   // A = U*sigma*V, where U, V are random orthogonal matrices from the
  350.   // Haar distribution.
  351.   A = qmult(sigma');
  352.   A = qmult(A');
  353.  
  354.   if (kl < n-1 || ku < n-1)    // Bandwidth reduction.
  355.   {
  356.    A = bandred(A, kl, ku);
  357.   }
  358.  
  359.   rand("default");
  360.   return A;
  361. };
  362.  
  363. //-------------------------------------------------------------------//
  364.  
  365. // Synopsis:    Band reduction by two-sided unitary transformations.
  366.  
  367. // Syntax:    bandred ( A , KL, KU )
  368.  
  369. // Description:
  370.  
  371. //    bandred(A, KL, KU) is a matrix unitarily equivalent to A with
  372. //    lower bandwidth KL and upper bandwidth KU (i.e. B(i,j) = 0 if
  373. //    i > j+KL or j > i+KU).  The reduction is performed using
  374. //    Householder transformations. If KU is omitted it defaults to
  375. //    KL. 
  376.  
  377. //    Called by randsvd.
  378. //    This is a `standard' reduction.  Cf. reduction to bidiagonal
  379. //    form prior to computing the SVD.  This code is a little
  380. //    wasteful in that it computes certain elements which are
  381. //    immediately set to zero! 
  382. //
  383. //      Reference:
  384. //      G.H. Golub and C.F. Van Loan, Matrix Computations, second edition,
  385. //      Johns Hopkins University Press, Baltimore, Maryland, 1989.
  386. //      Section 5.4.3.
  387.  
  388. //    This file is a translation of bandred.m from version 2.0 of
  389. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  390. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  391.  
  392. // Dependencies
  393. #   rfile house
  394.  
  395. //-------------------------------------------------------------------//
  396.  
  397. bandred = function ( A , kl , ku )
  398. {
  399.   local (A, kl, ku)
  400.  
  401.   if (!exist (ku)) { ku = kl; else ku = ku; }
  402.  
  403.   if (kl == 0 && ku == 0) {
  404.     error ("You''ve asked for a diagonal matrix.  In that case use the SVD!");
  405.   }
  406.  
  407.   // Check for special case where order of left/right transformations matters.
  408.   // Easiest approach is to work on the transpose, flipping back at the end.
  409.  
  410.   flip = 0;
  411.   if (ku == 0)
  412.   {
  413.     A = A';
  414.     temp = kl; kl = ku; ku = temp; flip = 1;
  415.   }
  416.  
  417.   m = A.nr; n = A.nc; 
  418.  
  419.   for (j in 1 : min( min(m, n), max(m-kl-1, n-ku-1) ))
  420.   {
  421.     if (j+kl+1 <= m)
  422.     {
  423.        ltmp = house(A[j+kl:m;j]);
  424.        beta = ltmp.beta; v = ltmp.v;
  425.        temp = A[j+kl:m;j:n];
  426.        A[j+kl:m;j:n] = temp - beta*v*(v'*temp);
  427.        A[j+kl+1:m;j] = zeros(m-j-kl,1);
  428.     }
  429.  
  430.     if (j+ku+1 <= n)
  431.     {
  432.        ltmp = house(A[j;j+ku:n]');
  433.        beta = ltmp.beta; v = ltmp.v;
  434.        temp = A[j:m;j+ku:n];
  435.        A[j:m;j+ku:n] = temp - beta*(temp*v)*v';
  436.        A[j;j+ku+1:n] = zeros(1,n-j-ku);
  437.     }
  438.   }
  439.  
  440.   if (flip) {
  441.     A = A';
  442.   }
  443.  
  444.   return A;
  445. };
  446.  
  447. //-------------------------------------------------------------------//
  448.  
  449. // Synopsis:    Lehmer matrix - symmetric positive definite.
  450.  
  451. // Syntax:      A = lehmer ( N )
  452.  
  453. // Description:
  454.  
  455. //      A is the symmetric positive definite N-by-N matrix with
  456. //                     A(i,j) = i/j for j >= i.
  457. //      A is totally nonnegative.  INV(A) is tridiagonal, and explicit
  458. //      formulas are known for its entries. 
  459.  
  460. //      N <= COND(A) <= 4*N*N.
  461.  
  462. //      References:
  463. //        M. Newman and J. Todd, The evaluation of matrix inversion
  464. //           programs, J. Soc. Indust. Appl. Math., 6 (1958), pp. 466-476.
  465. //        Solutions to problem E710 (proposed by D.H. Lehmer): The inverse
  466. //           of a matrix, Amer. Math. Monthly, 53 (1946), pp. 534-535.
  467. //        J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
  468. //           Birkhauser, Basel, and Academic Press, New York, 1977, p. 154.
  469.  
  470. //    This file is a translation of lehmer.m from version 2.0 of
  471. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  472. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  473.  
  474. //-------------------------------------------------------------------//
  475.  
  476. lehmer = function ( n )
  477. {
  478.   local (n)
  479.   global (tril)
  480.  
  481.   A = ones(n,1)*(1:n);
  482.   A = A./A';
  483.   A = tril(A) + tril(A,-1)';
  484.  
  485.   return A;
  486. };
  487.  
  488. //-------------------------------------------------------------------//
  489.  
  490. // Synopsis:    Pre-multiply by random orthogonal matrix.
  491.  
  492. // Syntax:    B = qmult ( A )
  493.  
  494. // Description:
  495.  
  496. //    B is Q*A where Q is a random real orthogonal matrix from the
  497. //    Haar distribution, of dimension the number of rows in
  498. //    A. Special case: if A is a scalar then QMULT(A) is the same as
  499.  
  500. //        qmult(eye(a))
  501.  
  502. //       Called by RANDSVD.
  503.  
  504. //       Reference:
  505. //       G.W. Stewart, The efficient generation of random
  506. //       orthogonal matrices with an application to condition estimators,
  507. //       SIAM J. Numer. Anal., 17 (1980), 403-409.
  508.  
  509. //    This file is a translation of qmult.m from version 2.0 of
  510. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  511. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  512.  
  513. //-------------------------------------------------------------------//
  514.  
  515. qmult = function ( A )
  516. {
  517.   local (A)
  518.  
  519.   n = A.nr; m = A.nc;
  520.  
  521.   //  Handle scalar A.
  522.   if (max(n,m) == 1)
  523.   {
  524.     n = A;
  525.     A = eye(n,n);
  526.   }
  527.  
  528.   d = zeros(n,n);
  529.  
  530.   for (k in n-1:1:-1)
  531.   {
  532.     // Generate random Householder transformation.
  533.     rand("normal", 0, 1);
  534.     x = rand(n-k+1,1);
  535.     s = norm(x, "2");
  536.     sgn = sign(x[1]) + (x[1]==0);    // Modification for sign(1)=1.
  537.     s = sgn*s;
  538.     d[k] = -sgn;
  539.     x[1] = x[1] + s;
  540.     beta = s*x[1];
  541.  
  542.     // Apply the transformation to A.
  543.     y = x'*A[k:n;];
  544.     A[k:n;] = A[k:n;] - x*(y/beta);
  545.   }
  546.  
  547.   // Tidy up signs.
  548.   for (i in 1:n-1)
  549.   {
  550.     A[i;] = d[i]*A[i;];
  551.   }
  552.  
  553.   A[n;] = A[n;]*sign(rand());
  554.   B = A;
  555.  
  556.   rand("default");
  557.   return B;
  558. };
  559.  
  560. //-------------------------------------------------------------------//
  561.  
  562. // Synopsis:    Create a Householder matrix.
  563.  
  564. // Syntax:    house ( X )
  565.  
  566. //    If HOUSE(x), which returns a list containing elements `v' and
  567. //    `beta',  then H = EYE - beta*v*v' is a Householder matrix such
  568. //    that Hx = -sign(x(1))*norm(x)*e_1. 
  569. //    NB: If x = 0 then v = 0, beta = 1 is returned.
  570. //            x can be real or complex.
  571. //            sign(x) := exp(i*arg(x)) ( = x./abs(x) when x ~= 0).
  572.  
  573. //    Theory: (textbook references Golub & Van Loan 1989, 38-43;
  574. //             Stewart 1973, 231-234, 262; Wilkinson 1965, 48-50).
  575. //      Hx = y: (I - beta*v*v')x = -s*e_1.
  576. //      Must have |s| = norm(x), v = x+s*e_1, and
  577. //      x'y = x'Hx =(x'Hx)' real => arg(s) = arg(x(1)).
  578. //      So take s = sign(x(1))*norm(x) (which avoids cancellation).
  579. //      v'v = (x(1)+s)^2 + x(2)^2 + ... + x(n)^2
  580. //          = 2*norm(x)*(norm(x) + |x(1)|).
  581.  
  582. //    References:
  583. //        G.H. Golub and C.F. Van Loan, Matrix Computations, second edition,
  584. //           Johns Hopkins University Press, Baltimore, Maryland, 1989.
  585. //        G.W. Stewart, Introduction to Matrix Computations, Academic Press,
  586. //           New York, 1973,
  587. //        J.H. Wilkinson, The Algebraic Eigenvalue Problem, Oxford University
  588. //           Press, 1965.
  589.  
  590. //    This file is a translation of house.m from version 2.0 of
  591. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  592. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  593.  
  594. //-------------------------------------------------------------------//
  595.  
  596. house = function ( x )
  597. {
  598.   local (x)
  599.  
  600.   m = x.nr; n = x.nc;
  601.   if (n > 1) { error ("Argument must be a column vector."); }
  602.  
  603.   s = norm(x,"2") * (sign(x[1]) + (x[1]==0)); // Modification for sign(0)=1.
  604.   v = x;
  605.   if (s == 0)    // Quit if x is the zero vector.
  606.   {
  607.     beta = 1; 
  608.     return << beta = beta ; v = v >>;
  609.   }
  610.  
  611.   v[1] = v[1] + s;
  612.   beta = 1/(s'*v[1]);        // NB the conjugated s.
  613.  
  614.   // beta = 1/(abs(s)*(abs(s)+abs(x(1)) would guarantee beta real.
  615.   // But beta as above can be non-real (due to rounding) only 
  616.   // when x is complex.
  617.  
  618.   return << beta = beta ; v = v >>;
  619. };
  620.  
  621. //-------------------------------------------------------------------//
  622.  
  623. //  Syntax:    tril ( A )
  624. //        tril ( A , K )
  625.  
  626. //  Description:
  627.  
  628. //  tril(x) returns the lower triangular part of A.
  629.  
  630. //  tril(A,K) returns the elements on and below the K-th diagonal of
  631. //  A.
  632.  
  633. //  K = 0: main diagonal
  634. //  K > 0: above the main diag.
  635. //  K < 0: below the main diag.
  636.  
  637. //  See Also: triu
  638. //-------------------------------------------------------------------//
  639.  
  640. tril = function(x, k) 
  641. {
  642.   local(x, k)
  643.  
  644.   if (!exist (k)) { k = 0; }
  645.   nr = x.nr; nc = x.nc;
  646.   if(k > 0) 
  647.   { 
  648.     if (k > (nc - 1)) { error ("tril: invalid value for k"); }
  649.   else
  650.     if (abs (k) > (nr - 1)) { error ("tril: invalid value for k"); }
  651.   }
  652.  
  653.   y = zeros(nr, nc);
  654.  
  655.   for(i in max( [1,1-k] ):nr) {
  656.     j = 1:min( [nc, i+k] );
  657.     y[i;j] = x[i;j];
  658.   }
  659.  
  660.   return y;
  661. };
  662.  
  663. //-------------------------------------------------------------------//
  664.  
  665. //  Syntax:    triu ( A )
  666. //        triu ( A , K )
  667.  
  668. //  Description:
  669.  
  670. //  triu(x) returns the upper triangular part of A.
  671.  
  672. //  tril(x; k) returns the elements on and above the k-th diagonal of
  673. //  A. 
  674.  
  675. //  K = 0: main diagonal
  676. //  K > 0: above the main diag.
  677. //  K < 0: below the main diag.
  678.  
  679. //  See Also: tril
  680. //-------------------------------------------------------------------//
  681.  
  682. triu = function(x, k) 
  683. {
  684.   local(x, k)
  685.  
  686.   if (!exist (k)) { k = 0; }
  687.   nr = x.nr; nc = x.nc;
  688.  
  689.   if(k > 0) 
  690.   { 
  691.     if (k > (nc - 1)) { error ("triu: invalid value for k"); }
  692.   else
  693.     if (abs (k) > (nr - 1)) { error ("triu: invalid value for k"); }
  694.   }
  695.  
  696.   y = zeros(nr, nc);
  697.  
  698.   for(j in max( [1,1+k] ):nc) {
  699.     i = 1:min( [nr, j-k] );
  700.     y[i;j] = x[i;j];
  701.   }
  702.  
  703.   return y;
  704. };
  705.  
  706. //
  707. //-------------------- Test Relational Expressions -------------------
  708. //
  709. printf("\tstart scalar tests...\n");
  710. printf("\tstart relational tests...\n");
  711.  
  712. //    SCALAR CONSTANTS (REAL)
  713. if( !(1<2) ) { error(); }
  714. if( !(1<=2) ) { error(); }
  715. if( 1>2 ) { error(); }
  716. if( 1>=2 ) { error(); }
  717. if( 1==2 ) { error(); }
  718. if( !(1!=2) ) { error(); }
  719. if( !1 ) { error(); }
  720. if( !!!1) { error(); }
  721.  
  722. if( !([1]<[2]) ) { error(); }
  723. if( !([1]<=[2]) ) { error(); }
  724. if( [1]>[2] ) { error(); }
  725. if( [1]>=[2] ) { error(); }
  726. if( [1]==[2] ) { error(); }
  727. if( !([1]!=[2]) ) { error(); }
  728. if( ![1] ) { error(); }
  729. if( !!![1]) { error(); }
  730.  
  731. //    SCALAR CONSTANTS (COMPLEX)
  732. if( !(1+2i<2+3i) ) { error(); }
  733. if( !(1+2i<=2+3i) ) { error(); }
  734. if( 1+2i>2+3i ) { error(); }
  735. if( 1+2i>=2+3i ) { error(); }
  736. if( 1+2i==2+3i ) { error(); }
  737. if( !(1+2i!=2+3i) ) { error(); }
  738. if( !1+2i ) { error(); }
  739. if( !!!1+2i) { error(); }
  740.  
  741. if( !([1+2i]<[2+3i]) ) { error(); }
  742. if( !([1+2i]<=[2+3i]) ) { error(); }
  743. if( [1+2i]>[2+3i] ) { error(); }
  744. if( [1+2i]>=[2+3i] ) { error(); }
  745. if( [1+2i]==[2+3i] ) { error(); }
  746. if( !([1+2i]!=[2+3i]) ) { error(); }
  747. if( ![1+2i] ) { error(); }
  748. if( !!![1+2i]) { error(); }
  749.  
  750. //    SCALAR ENTITIES (REAL)
  751. a=1;b=2;
  752. if( !(a<b) ) { error(); }
  753. if( !(a<=b) ) { error(); }
  754. if( a>b ) { error(); }
  755. if( a>=b ) { error(); }
  756. if( a==b ) { error(); }
  757. if( !(a!=b) ) { error(); }
  758. if( !a ) { error(); }
  759. if( !!!a) { error(); }
  760.  
  761. if( !([a]<[b]) ) { error(); }
  762. if( !([a]<=[b]) ) { error(); }
  763. if( [a]>[b] ) { error(); }
  764. if( [a]>=[b] ) { error(); }
  765. if( [a]==[b] ) { error(); }
  766. if( !([a]!=[b]) ) { error(); }
  767. if( ![a] ) { error(); }
  768. if( !!![a]) { error(); }
  769.  
  770. //    SCALAR ENTITIES (COMPLEX)
  771. a=1+2i;b=2+3i;
  772. if( !(a<b) ) { error(); }
  773. if( !(a<=b) ) { error(); }
  774. if( a>b ) { error(); }
  775. if( a>=b ) { error(); }
  776. if( a==b ) { error(); }
  777. if( !(a!=b) ) { error(); }
  778. if( !a ) { error(); }
  779. if( !!!a) { error(); }
  780.  
  781. if( !([a]<[b]) ) { error(); }
  782. if( !([a]<=[b]) ) { error(); }
  783. if( [a]>[b] ) { error(); }
  784. if( [a]>=[b] ) { error(); }
  785. if( [a]==[b] ) { error(); }
  786. if( !([a]!=[b]) ) { error(); }
  787. if( ![a] ) { error(); }
  788. if( !!![a]) { error(); }
  789.  
  790. x = rand(4,4);
  791.  
  792. if (! all (all (-2 <  x))) { "-2 < x" error (); }
  793. if (! all (all (-2 <= x))) { "-2 <= x" error (); }
  794. if (! all (all ( 2 >  x))) { "2 > x" error (); }
  795. if (! all (all ( 2 >= x))) { "2 >= x" error (); }
  796.  
  797. if (! all (all (rand(4,4) >  -2))) { error (); }
  798. if (! all (all (rand(4,4) >= -2))) { error (); }
  799. if (! all (all (rand(4,4) <   2))) { error (); }
  800. if (! all (all (rand(4,4) <=  2))) { error (); }
  801.  
  802. if (! all (all (rand (4,4) >  -rand (4,4)))) { error (); }
  803. if (! all (all (rand (4,4) >= -rand (4,4)))) { error (); }
  804. if (! all (all (-rand (4,4) <  rand (4,4)))) { error (); }
  805. if (! all (all (-rand (4,4) <= rand (4,4)))) { error (); }
  806.  
  807. //------------------------- Test REAL SCALARS ------------------------
  808. //
  809. //    CONSTANTS
  810. //      Addition
  811. if(1+2 != 3) { error(); }
  812. //      Subtraction
  813. if(1-2 != -1) { error(); }
  814. //      Multiply
  815. if(1*2 != 2) { error(); }
  816. //      Divide
  817. if(1/2 != 0.5) { error(); }
  818. //      Power
  819. if(2^2 != 4) { error(); }
  820. if(4^0 != 1) { error(); }
  821. //      Unary Minus
  822. if(2-3 != -1) { error(); }
  823. //
  824. //    ENTITIES
  825. //
  826. a = 1; b = 2; c = 3; d = 0.5;
  827. //      Addition
  828. if(a+b != c) { error(); }
  829. //      Subtraction
  830. if(a-b != -a) { error(); }
  831. //      Multiply
  832. if(a*b != b) { error(); }
  833. //      Divide
  834. if(a/b != d) { error(); }
  835. //      Power
  836. if(b^b != b*b) { error(); }
  837. if(b^0 != 1) { error (); }
  838. //      Unary Minus
  839. if(b-c != -a) { error(); }
  840. //
  841. //  ENTITIES & CONSTANTS
  842. //
  843. if(a+2 != c) { error(); }
  844. //      Subtraction
  845. if(1-b != -a) { error(); }
  846. //      Multiply
  847. if(a*2 != 2) { error(); }
  848. //      Divide
  849. if(1/b != d) { error(); }
  850. //      Power
  851. if(2^b != b*b) { error(); }
  852. //      Unary Minus
  853. if(b-3 != -a) { error(); }
  854. //
  855. //------------------------Test COMPLEX SCALARS -------------------------
  856. //
  857. //    CONSTANTS
  858. if(sqrt(-1) != 1i) { error(); }
  859. //      Addition
  860. if((1+2i)+(2+3i) != (3+5i)) { error(); }
  861. //      Subtraction
  862. if((1+2i)-(3+4i) != (-2-2i)) { error(); }
  863. //      Multiply
  864. if((1+2i)*(3+4i) != (-5+10i)) { error(); }
  865. //      Divide
  866. if((1+2i)/(3-4i) != (-.2+.4i)) { error(); }
  867. //      Power
  868. //      Precision problems prevent us from testing these. Have to
  869. //      be checked by hand.
  870. //  (1+2i)^2 = -3 + 4i
  871. //  (1+2i)^.5 = 1.272 + 7.862e-1i
  872. //  if((1+2i)^2 != (-3+4i)) { error(); }
  873. //  if((1+2i)^10 != (237+3116i)) { error(); }
  874. //      Unary Minus
  875. if(-(1+2i) != -1-2i) { error(); }
  876. //
  877. //    ENTITIES
  878. //
  879. a = 1+2i; b = 3+4i; c = 4+6i;
  880. if(a+b != c) { error(); }
  881. //      Subtraction
  882. if(a-b != -2-2i) { error(); }
  883. //      Multiply
  884. if(a*b != -5+10i) { error(); }
  885. //      Divide
  886. //if(a/(3-4i) != -.2+.4i) { error(); }
  887. //      Power
  888. //  if(b^b != b*b) { error(); }
  889. //      Unary Minus
  890. if(-a != -1-2i) { error(); }
  891. //
  892. //    ENTITIES & CONSTANTS
  893. //
  894. if(a+(3+4i) != c) { error(); }
  895. //      Subtraction
  896. if((1+2i)-b != -2-2i) { error(); }
  897. //      Multiply
  898. if(a*(3+4i) != -5+10i) { error(); }
  899. //      Divide
  900. //if(a/(3-4i) != -.2+.4i) { error(); }
  901. //      Power
  902. //if(b^b != b*b) { error(); }
  903. //      Unary Minus
  904. if(-(1+2i) != -1-2i) { error(); }
  905. //
  906. // String - Numerical Equalities
  907. //
  908.  
  909. if ((1 == "1")) { error(); }
  910. if (([1] == "1")) { error(); }
  911. if (("1" == 1)) { error(); }
  912. if (("1" == [1])) { error(); }
  913.  
  914. if (rand(3,3) == "str") { error(); }
  915.  
  916. if ("str" == rand(3,3)) { error(); }
  917. if (!any (any ((["1", "2"; "3", "4"] == "1") == [1,0;0,0]))) { error(); }
  918.  
  919. //
  920. //----------------------- Test REAL MATRICES ---------------------------
  921. //
  922.  
  923. printf("\tstart matrix tests...\n");
  924. printf("\t\treal-matrices\n");
  925.  
  926. //  Read in test matrices
  927. //
  928.  
  929. read("test_input");
  930.  
  931. //
  932. //  Matrix construction
  933. //
  934.  
  935. if(any([1;2;3] != [1,2,3]')) {
  936.   error();
  937. }
  938.  
  939. if(any (any (m0 != zeros(2,2)))) { error(); }
  940. if(any (any (m1 != 1+zeros(2,2)))) { error(); }
  941. if(any (any (m2 != [1,2;3,4]))) { error(); }
  942. if(any (any (m3 != [1+2i,2+3i;3+4i,5+6i]))) { error(); }
  943. if(any (any ([1,2;3+0i,4+0i] != m2))) { error(); }
  944. if(any (any ([m2] != m2))) { error(); }
  945.  
  946. //
  947. //  Matrix indexing
  948. //
  949.  
  950. p = pascal(6);
  951. if (!all (all (p[ [1:3] ; ] == p[ [1:3]' ; ]))) { error (); }
  952. if (!all (all (p[ ; [1:3] ] == p[ ; [1:3]' ]))) { error (); }
  953. if (!all (all (p[ [2:6] ; [2:6] ] == p[ [2:6]'; [2:6]' ]))) { error (); }
  954. if (!all (all (p[ [2:6]' ; [2:6] ] == p[ [2:6]'; [2:6]' ]))) { error (); }
  955. if (!all (all (p[ [6:12]' ] == p[ [6:12] ]))) { error (); }
  956.  
  957. //
  958. //  Sub-Matrix promotion
  959. //
  960.  
  961. if(m2[2;2] != 4) { error(); }
  962. if(any(m2[2;] != [3,4])) { error(); }
  963. if(any(m2[;2] != [2,4]')) { error(); }
  964. i=2;j=1;
  965. if(m2[i;j] != 3) { error(); }
  966. i=1;j=2;
  967. if(m2[i;j] != 2) { error(); }
  968. m = [1,2,3;4,5,6;7,8,9];
  969.  
  970. if(any(m[1;1,2] != [1,2])) 
  971. {
  972.   error();
  973. }
  974.  
  975. if(any(m[1,2;1] != [1;4])) 
  976. {
  977.   error();
  978. }
  979.  
  980. if(any (any (m[1,2;1,2] != [1,2;4,5]))) 
  981. {
  982.   error();
  983. }
  984.  
  985. //
  986.  
  987. if(m3[2;2] != (5+6i)) { error(); }
  988. if(any(m3[2;] != [3+4i,5+6i])) { error(); }
  989. if(any(m3[;2] != conj([2+3i,5+6i]'))) { error(); }
  990.  
  991. //
  992. //  Automatic creation, extension
  993. //
  994.  
  995. if(any (any ((mm[3;3]=10) != [0,0,0;0,0,0;0,0,10]))) { error(); }
  996. a=[1,2,3;4,5,6];
  997. if(any (any ((a[3;1]=10) != [1,2,3;4,5,6;10,0,0]))) { error(); }
  998. a=[1,2;3,4];
  999. if(any (any ((a[3,4;3,4]=[5,6;7,8]) != [1,2,0,0;3,4,0,0;0,0,5,6;0,0,7,8]))) 
  1000. {
  1001.   error();
  1002. }
  1003.  
  1004. mmm[2;] = a[1;];
  1005.  
  1006. //
  1007. //  Matrix binary operations
  1008. //
  1009.  
  1010. a = m2; b = [5,6;7,8];
  1011. if(any (any (a+a != [2,4;6,8]))) { error(); }
  1012. if(any (any (a-a != zeros(2,2)))) { error(); }
  1013. if(any (any (2+a != [3,4;5,6]))) { error(); }
  1014. if(any (any (2-a != [1,0;-1,-2]))) { error(); }
  1015. if(any (any (a-2 != [-1,0;1,2]))) { error(); }
  1016. if(any (any (2*a != [2,4;6,8]))) { error(); }
  1017. if(any (any ((a./2 != [0.5,1;1.5,2])))) { error(); }
  1018. if(any (any (a*a != [7,10;15,22]))) { error(); }
  1019. if(any (any (a*a*a != [37,54;81,118]))) { error(); }
  1020. if(any (any (a .* a != [1,4;9,16]))) { error(); }
  1021. if(any (any (a./a != [1,1;1,1]))) { error(); }
  1022. if(any (any (a' != [1,3;2,4]))) { error(); }
  1023.  
  1024. if(any(any(rand(3,3)^0 != eye(3,3)))) { error(); }
  1025. if(any(any(rand(3,3).^0 != ones(3,3)))) { error(); }
  1026. if(any(any(rand(1,3).^0 != ones(1,3)))) { error(); }
  1027. if(any(any(rand(3,1).^0 != ones(3,1)))) { error(); }
  1028. if(any(any(1.^zeros(3,1) != ones(3,1)))) { error(); }
  1029. if(any(any(1.^zeros(1,3) != ones(1,3)))) { error(); }
  1030.  
  1031. if(any ([1;2;3]+[4;5;6] != [5;7;9])) 
  1032. {
  1033.   error();
  1034. }
  1035.  
  1036. if(any ([1;2;3]-[4;5;6] != [-3;-3;-3])) 
  1037. {
  1038.   error();
  1039. }
  1040.  
  1041. if(any ([2;2;2] ./ [1;1;1] != [2;2;2])) 
  1042. {
  1043.   error();
  1044. }
  1045.  
  1046. if(any ([1;2;3] .* [4;5;6] != [4;10;18])) 
  1047. {
  1048.   error();
  1049. }
  1050.  
  1051. if (type (1^.33333) != "real") { error (); }
  1052. if (type (1^(1/3)) != "real") { error (); }
  1053. if (type ([1]^.33333) != "real") { error (); }
  1054. if (type (1^[.33333]) != "real") { error (); }
  1055. if (type ([1]^[.33333]) != "real") { error (); }
  1056.  
  1057. //
  1058. // Test row-wise matrix addition
  1059. //
  1060.  
  1061. a = [1,2,3]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1062. if (!all (all (a .+ b == [2,4,6;5,7,9;8,10,12;11,13,15]))) { error (); }
  1063. if (!all (all (b .+ a == [2,4,6;5,7,9;8,10,12;11,13,15]))) { error (); }
  1064.  
  1065. a = [1,2,3] + [1,2,3]*1i; 
  1066. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1067. c = [2,4,6;5,7,9;8,10,12;11,13,15] + [2,4,6;5,7,9;8,10,12;11,13,15]*1i;
  1068. if (!all (all (a .+ b == c))) { error (); }
  1069. if (!all (all (b .+ a == c))) { error (); }
  1070.  
  1071. printf("\t\tpassed matrix row-wise add test...\n");
  1072.  
  1073. //
  1074. // Test row-wise matrix subtraction
  1075. //
  1076.  
  1077. a = [1,1,1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1078. if (!all (all (a .- b == -[0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1079. if (!all (all (b .- a ==  [0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1080.  
  1081. a = [1,1,1] + [1,1,1]*1i;
  1082. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1083. c = [0,1,2;3,4,5;6,7,8;9,10,11] + [0,1,2;3,4,5;6,7,8;9,10,11]*1i;
  1084. if (!all (all (a .- b == -c))) { error (); }
  1085. if (!all (all (b .- a ==  c))) { error (); }
  1086.  
  1087. printf("\t\tpassed matrix row-wise subtraction test...\n");
  1088.  
  1089. //
  1090. // Test col-wise matrix addition
  1091. //
  1092.  
  1093. a = [1;1;1;1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1094. if (!all (all (a .+ b == [2,3,4;5,6,7;8,9,10;11,12,13]))) { error (); }
  1095. if (!all (all (b .+ a == [2,3,4;5,6,7;8,9,10;11,12,13]))) { error (); }
  1096.  
  1097. a = [1;1;1;1] + [1;1;1;1]*1i;
  1098. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1099. c = [2,3,4;5,6,7;8,9,10;11,12,13] + [2,3,4;5,6,7;8,9,10;11,12,13]*1i;
  1100. if (!all (all (a .+ b == c))) { error (); }
  1101. if (!all (all (b .+ a == c))) { error (); }
  1102.  
  1103. printf("\t\tpassed matrix col-wise add test...\n");
  1104.  
  1105. //
  1106. // Test col-wise matrix subtraction
  1107. //
  1108.  
  1109. a = [1;1;1;1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1110. if (!all (all (a .- b == -[0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1111. if (!all (all (b .- a ==  [0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1112.  
  1113. a = [1;1;1;1] + [1;1;1;1]*1i;
  1114. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1115. c = [0,1,2;3,4,5;6,7,8;9,10,11] + [0,1,2;3,4,5;6,7,8;9,10,11]*1i;
  1116. if (!all (all (a .- b == -c))) { error (); }
  1117. if (!all (all (b .- a ==  c))) { error (); }
  1118.  
  1119. printf("\t\tpassed matrix col-wise subtraction test...\n");
  1120.  
  1121. a = [1,2,3];
  1122. b = [1,2,3;4,5,6;7,8,9];
  1123. c = [1,4,9;4,10,18;7,16,27];
  1124.  
  1125. if (!all (all (a .* b == c))) { error (); }
  1126. if (!all (all (b .* a == c))) { error (); }
  1127.  
  1128. za = a + rand (size (a))*1j;
  1129. zb = b + rand (size (b))*1j;
  1130.  
  1131. if (!all (all (za .* zb == [za;za;za] .* zb))) { error (); }
  1132. if (!all (all (zb .* za == zb .* [za;za;za]))) { error (); }
  1133. if (!all (all (a .* zb == [a;a;a] .* zb))) { error (); }
  1134. if (!all (all (zb .* a == zb .* [a;a;a]))) { error (); }
  1135. if (!all (all (za .* b == [za;za;za] .* b))) { error (); }
  1136. if (!all (all (b .* za == b .* [za;za;za]))) { error (); }
  1137.  
  1138. printf("\t\tpassed matrix row-wise multiplication test...\n");
  1139.  
  1140. a = [1,2,3];
  1141. b = [1,2,3;4,6,6;7,8,9];
  1142. c = [1,1,1;4,3,2;7,4,3];
  1143.  
  1144. if (!all (all (b ./ a == c))) { error (); }
  1145. if (!all (all ([a;a;a] ./ b == a ./ b))) { error (); }
  1146. if (!all (all (b ./ [a;a;a] == b ./ a))) { error (); }
  1147.  
  1148. za = a + rand (size (a))*1j;
  1149. zb = b + rand (size (b))*1j;
  1150.  
  1151. if (!all (all ([za;za;za] ./ zb == za ./ zb))) { error (); }
  1152. if (!all (all (zb ./ [za;za;za] == zb ./ za))) { error (); }
  1153. if (!all (all ([a;a;a] ./ zb == a ./ zb))) { error (); }
  1154. if (!all (all (zb ./ [a;a;a] == zb ./ a))) { error (); }
  1155. if (!all (all ([za;za;za] ./ b == za ./ b))) { error (); }
  1156. if (!all (all (b ./ [za;za;za] == b ./ za))) { error (); }
  1157.  
  1158. printf("\t\tpassed matrix row-wise division test...\n");
  1159.  
  1160. a = [1;2;3];
  1161. b = [1,2,3;4,5,6;7,8,9];
  1162.  
  1163. if (!all (all (a .* b == [a,a,a] .* b))) { error (); }
  1164. if (!all (all (b .* a == b .* [a,a,a]))) { error (); }
  1165.  
  1166. za = a + rand (size (a))*1j;
  1167. zb = b + rand (size (b))*1j;
  1168.  
  1169. if (!all (all (za .* zb == [za,za,za] .* zb))) { error (); }
  1170. if (!all (all (zb .* za == zb .* [za,za,za]))) { error (); }
  1171. if (!all (all (za .* b == [za,za,za] .* b))) { error (); }
  1172. if (!all (all (b .* za == b .* [za,za,za]))) { error (); }
  1173. if (!all (all (a .* zb == [a,a,a] .* zb))) { error (); }
  1174. if (!all (all (zb .* a == zb .* [a,a,a]))) { error (); }
  1175.  
  1176. printf("\t\tpassed matrix column-wise multiplication test...\n");
  1177.  
  1178. a = [1;2;3];
  1179. b = [1,2,3;4,6,6;7,8,9];
  1180.  
  1181. if (!all (all ([a,a,a] ./ b == a ./ b))) { error (); }
  1182. if (!all (all (b ./ [a,a,a] == b ./ a))) { error (); }
  1183.  
  1184. za = a + rand (size(a))*1j;
  1185. zb = b + rand (size(b))*1j;
  1186.  
  1187. if (!all (all ([za,za,za] ./ zb == za ./ zb))) { error (); }
  1188. if (!all (all (zb ./ [za,za,za] == zb ./ za))) { error (); }
  1189. if (!all (all ([za,za,za] ./ b == za ./ b))) { error (); }
  1190. if (!all (all (b ./ [za,za,za] == b ./ za))) { error (); }
  1191. if (!all (all ([a,a,a] ./ zb == a ./ zb))) { error (); }
  1192. if (!all (all (zb ./ [a,a,a] == zb ./ a))) { error (); }
  1193.  
  1194. printf("\t\tpassed matrix column-wise division test...\n");
  1195.  
  1196.  
  1197. //
  1198. //--------------------- Test COMPLEX MATRICES --------------------------
  1199. //
  1200. //  Automatic creation, extension
  1201. //
  1202. printf("\t\tcomplex-matrices\n");
  1203. if(any (any ((mm[3;3]=10+10i) != [0,0,0;0,0,0;0,0,10+10i]))) { error(); }
  1204. a=[1,2,3;4,5,6];
  1205. if(any (any ((a[3;1]=10+10i) != [1,2,3;4,5,6;10+10i,0,0]))) { error(); }
  1206. //
  1207. a = m3;
  1208. if(any (any (a+a != [2+4i,4+6i;6+8i,10+12i]))) { error(); }
  1209. if(any (any (a-a != zeros(2,2)))) { error(); }
  1210. if(any (any (2+a != [3+2i,4+3i;5+4i,7+6i]))) { "2+a" error(); }
  1211. if(any (any (2-a != [1-2i,0-3i;-1-4i,-3-6i]))) { error(); }
  1212. if(any (any (a-2 != [-1+2i,0+3i;1+4i,3+6i]))) { error(); }
  1213. if(any (any (2*a != [2+4i,4+6i;6+8i,10+12i]))) { error(); }
  1214. if(any (any (a./2 != [.5+1i,1+1.5i;1.5+2i,2.5+3i]))) { error(); }
  1215. if(any (any (a*a != [-9+21i,-12+34i;-14+48i,-17+77i]))) { error(); }
  1216. if(any (any (a*a*a != [-223+57i,-345+113i;-469+183i,-719+337i]))) { error(); }
  1217. if(any (any (a .* a != [-3+4i,-5+12i;-7+24i,-11+60i]))) { error(); }
  1218. //
  1219. // The following test may not work on some machines
  1220. //
  1221. if(any (any (a./a != [1,1;1,1]))) { 
  1222.   printf("\t\t***complex division inaccuracy, check manually***\n");
  1223. }
  1224.  
  1225. if(any (any (a' != conj([1+2i,3+4i;2+3i,5+6i])))) { error(); }
  1226. //  
  1227. //--------------------- Test NULL MATRICES -------------------------
  1228. //
  1229. printf("\t\tnull-matrices\n");
  1230. // Create a NULL matrix
  1231. mnull = [];
  1232. // Test it with SCALARS
  1233. if( any([1,mnull] != 1)) {
  1234.   error();
  1235. }
  1236. if( any([mnull,1] != 1)) {
  1237.   error();
  1238. }
  1239. // Test with MATRIX construction
  1240. m = [1,2;3,4;5,6];
  1241. if( any([mnull;1] != [1])) {
  1242.   error();
  1243. }
  1244. if( any([1;mnull] != [1])) {
  1245.   error();
  1246. }
  1247. if( any([mnull;1,2,3] != [1,2,3])) {
  1248.   error();
  1249. }
  1250. if( any([1,2,3;mnull] != [1,2,3])) {
  1251.   error();
  1252. }
  1253. if(any (any ([mnull,m] != m))) {
  1254.   error();
  1255. }
  1256. if(any (any ([m,mnull] != m))) {
  1257.   error();
  1258. }
  1259. if(any (any ([mnull;m] != m))) {
  1260.   error();
  1261. }
  1262. if(any (any ([m;mnull] != m))) {
  1263.   error();
  1264.  
  1265. mnull = matrix();
  1266. mnull[1] = [1];
  1267. }
  1268.  
  1269. //--------------------- Test Matrix Multiply --------------------------
  1270.  
  1271. i = sqrt(-1);
  1272. a = [1,2,3;4,5,6;7,8,9];
  1273. b = [4,5,6;7,8,9;10,11,12];
  1274. c = [ 48,  54,  60 ;
  1275.      111, 126, 141 ;
  1276.      174, 198, 222 ];
  1277.  
  1278. if (any (any (c != a*b))) { error ("failed Real-Real Multiply"); }
  1279.  
  1280. az = a + b*i;
  1281. bz = b + a*i;
  1282.  
  1283. cz = [-18+141*i , -27+162*i , -36+183*i ;
  1284.         9+240*i ,   0+279*i ,  -9+318*i ;
  1285.        36+339*i ,  27+396*i ,  18+453*i ];
  1286.  
  1287. czz = [ 48+30*i ,  54+36*i  ,  60+42*i ;
  1288.        111+66*i , 126+81*i  , 141+96*i ;
  1289.        174+102*i, 198+126*i , 222+150*i ];
  1290.  
  1291. czzz = [ 48+111*i ,  54+126*i ,  60+141*i ;
  1292.         111+174*i , 126+198*i , 141+222*i ;
  1293.         174+237*i , 198+270*i , 222+303*i ];
  1294.  
  1295. if (any (any (cz != az*bz)))  { error ("failed Complex-Complex Multiply"); }
  1296. if (any (any (czz != a*bz)))  { error ("failed Real-Complex Multiply"); }
  1297. if (any (any (czzz != az*b))) { error ("failed Complex-Real Multiply"); }
  1298.  
  1299. a = [a,a];
  1300. b = [b;b];
  1301. c = [  96 , 108 , 120 ;
  1302.       222 , 252 , 282 ;
  1303.       348 , 396 , 444 ];
  1304.  
  1305. if (any (any (c != a*b))) { error ("failed Real-Real Multiply"); }
  1306.  
  1307. az = [az,az];
  1308. bz = [bz;bz];
  1309.  
  1310. cz = [  -36+282*i ,  -54+324*i ,  -72+366*i ;
  1311.          18+480*i ,    0+558*i ,  -18+636*i ;
  1312.          72+678*i ,   54+792*i ,   36+906*i ];
  1313.  
  1314. czz = [  96+60*i  , 108+72*i  , 120+84*i  ;
  1315.         222+132*i , 252+162*i , 282+192*i ;
  1316.         348+204*i , 396+252*i , 444+300*i ];
  1317.  
  1318. czzz = [  96+222*i , 108+252*i , 120+282*i ;
  1319.          222+348*i , 252+396*i , 282+444*i ;
  1320.          348+474*i , 396+540*i , 444+606*i ];
  1321.  
  1322. if (any (any (cz != az*bz)))  { error ("failed Complex-Complex Multiply"); }
  1323. if (any (any (czz != a*bz)))  { error ("failed Real-Complex Multiply"); }
  1324. if (any (any (czzz != az*b))) { error ("failed Complex-Real Multiply"); }
  1325.  
  1326. printf("\t\tpassed matrix multiply test...\n");
  1327.  
  1328. //--------------------- Test STRING MATRICES --------------------------
  1329. //
  1330. printf("\t\tstring-matrices\n");
  1331. sm = ["s1","sm2","sm3";"x1","x2","xxx3";"y1","y2","yyy3"];
  1332. if(sm[1] != "s1") { error(); }
  1333. if( sm[1;3] != "sm3" ) { error(); }
  1334. if(any(sm[2,3;3] != ["xxx3";"yyy3"]) ) { error(); }
  1335. if(any (any ((sm[1;1]="xx")!=["xx","sm2","sm3";"x1","x2","xxx3";"y1","y2","yyy3"])))
  1336. {
  1337.   error();
  1338. }
  1339. if( ((strm[1] = "strm")[1]) != "strm" ) { error(); }
  1340.  
  1341. //  Test string-matrix add functionality
  1342.  
  1343. sm = sm[1,2;1,2];
  1344. if (any (any (("1_"+sm+"_2") != ["1_xx_2","1_sm2_2";"1_x1_2","1_x2_2"]))) {error();}
  1345.  
  1346. if ("c"+["1"] != "c1") { error (); }
  1347. if (["c"]+"1" != "c1") { error (); }
  1348.  
  1349. // An append test...
  1350. bs = "b";
  1351. bm = "b";
  1352.  
  1353. xsm = ["a", bm, bs ];
  1354. if (!all(xsm == ["a", "b", "b"])) { error (); }
  1355.  
  1356. printf("\tpassed matrix test...\n");
  1357.  
  1358. //
  1359. //---------------------------- List Tests --------------------------
  1360. //
  1361. //  List creation
  1362. listest = << << 11; 12 >>; << 21; 22>> >>;
  1363. if( listest.[1].[2] != 12 ) { error(); }
  1364. if(any(<<a=10;b=1:4;c=[1,2,3;4,5,6;7,8,9]>>.b != [1,2,3,4])) { error(); }
  1365. mlist.[0] = m;
  1366. if(any(any(mlist.[0] != m))) { error(); }
  1367.  
  1368. // Test list functions...
  1369.  
  1370. listtest.fun = function ( a ) { return sin(a); };
  1371. if (!(listtest.fun(0.5) == sin(0.5))) { error() ; }
  1372.  
  1373. // Test open-list assignment...
  1374. </v;d/> = eig(rand(3,3));
  1375.  
  1376. printf("\tpassed list test...\n");
  1377.  
  1378. //
  1379. // Reset random number generator seed...
  1380. //
  1381.  
  1382. rand("default");
  1383.  
  1384. //
  1385. //-------------------------- Test printf () --------------------------
  1386. //
  1387.  
  1388. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", 5,3,2, 8,7,3, "string", 3, 4, 1234e-2);
  1389. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1390.  
  1391. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", [5],[3],[2], [8],[7],[3], ...
  1392.          ["string"], [3], [4], [1234e-2]);
  1393. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1394.  
  1395. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", [5,1],[3,2],[2,2], [8],[7],[3], ...
  1396.          ["string"], [3], [4], [1234e-2,4]);
  1397. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1398.  
  1399. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", [5+2i,1],[3,2+4i],[2,2], [8],[7],[3], ...
  1400.          ["string"], [3+2i], [4], [1234e-2+12j,4]);
  1401. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1402.  
  1403. printf("\tpassed sprintf test...\n");
  1404.  
  1405. //
  1406. //------------------------- Test strtod()  ----------------------------
  1407. //
  1408.  
  1409. if (123.456 != strtod ("123.456")) { error (); }
  1410. if (!all (all ([1,2;3,4] == strtod (["1","2";"3","4"]))))
  1411.   { error (); }
  1412. printf("\tpassed strtod test...\n");
  1413.  
  1414. //
  1415. //------------------------- Test getline()  ---------------------------
  1416. //
  1417. //
  1418.  
  1419. close( "test_getl" );
  1420.  
  1421. x = getline( "test_getl" );
  1422. if ( x.[1] !=  123.456 ) { error(); }
  1423. if ( x.[2] != -123.456 ) { error(); }
  1424. if ( x.[3] !=  123.456 ) { error(); }
  1425.  
  1426. x = getline( "test_getl" );
  1427. if ( x.[1] !=  .123 ) { error(); }
  1428. if ( x.[2] != -.123 ) { error(); }
  1429. if ( x.[3] !=  .123 ) { error(); }
  1430.  
  1431. x = getline( "test_getl" );
  1432. if ( x.[1] !=  123 ) { error(); }
  1433. if ( x.[2] != -123 ) { error(); }
  1434. if ( x.[3] !=  123 ) { error(); }
  1435.  
  1436. x = getline( "test_getl" );
  1437. if ( x.[1] !=  1e6 ) { error(); }
  1438. if ( x.[2] != -1e6 ) { error(); }
  1439. if ( x.[3] !=  1e6 ) { error(); }
  1440.  
  1441. x = getline( "test_getl" );
  1442. if ( x.[1] !=  1e5 ) { error(); }
  1443. if ( x.[2] != -1e5 ) { error(); }
  1444. if ( x.[3] !=  1e5 ) { error(); }
  1445.  
  1446. x = getline( "test_getl" );
  1447. if ( x.[1] !=  123.456e3 ) { error(); }
  1448. if ( x.[2] != -123.456e3 ) { error(); }
  1449. if ( x.[3] !=  123.456e3 ) { error(); }
  1450.  
  1451. x = getline( "test_getl" );
  1452. if ( x.[1] !=  123.456e3 ) { error(); }
  1453. if ( x.[2] != -123.456e3 ) { error(); }
  1454. if ( x.[3] !=  123.456e3 ) { error(); }
  1455.  
  1456. x = getline( "test_getl" );
  1457. if ( x.[1] !=  123.456e-3 ) { error(); }
  1458. if ( x.[2] != -123.456e-3 ) { error(); }
  1459. if ( x.[3] !=  123.456e-3 ) { error(); }
  1460.  
  1461. x = getline( "test_getl" );
  1462. if ( x.[1] !=  .123e3 ) { error(); }
  1463. if ( x.[2] != -.123e3 ) { error(); }
  1464. if ( x.[3] !=  .123e3 ) { error(); }
  1465.  
  1466. x = getline( "test_getl" );
  1467. if ( x.[1] !=  123e3 ) { error(); }
  1468. if ( x.[2] != -123e3 ) { error(); }
  1469. if ( x.[3] !=  123e3 ) { error(); }
  1470.  
  1471. x = getline( "test_getl" );
  1472. if ( x.[1] != "123abc" ) { error(); }
  1473. if ( x.[2] != "abc123" ) { error(); }
  1474. if ( x.[3] != "123.abc" ) { error(); }
  1475.  
  1476. x = getline( "test_getl" );
  1477. if ( x.[1] != "quoted string" ) { error(); }
  1478. if ( x.[2] != "q string with escapes \n \t \" " ) { error(); }
  1479.  
  1480. x = getline( "test_getl" );
  1481. if ( x.[1] != "quoted string" ) { error(); }
  1482. if ( x.[2] !=  1.23e3 ) { error(); }
  1483. if ( x.[3] !=  100 ) { error(); }
  1484. if ( x.[4] != "q string with escapes \n \t \" " ) { error(); }
  1485. if ( x.[5] !=  200 ) { error(); }
  1486.  
  1487. x = getline ("test_getl", 0);
  1488. if (type (x) != "string") { error (); }
  1489.  
  1490. // Also check out strsplt while we are here...
  1491. if (length (strsplt(x)) != 79) { error (); }
  1492. if (length (strsplt(x, 13)) != 6) { error (); }
  1493. if (length (strsplt(x,[13,12,14,13,15,11] )) != 6) { error (); }
  1494. if (length (strsplt(x, ".")) != 7) { error (); }
  1495.  
  1496. printf("\tpassed getline() test...\n");
  1497.  
  1498. //
  1499. //---------------------- Test readb()/writeb()  --------------------
  1500. //
  1501.  
  1502. a = rand (5,5);
  1503. z = rand(3,3) + rand(3,3)*1j;
  1504. strm = what()[1:5;1:5];
  1505. pi = 4*atan(1.0);
  1506. sc = 2*pi;
  1507. str = "this is a sample string\ttab";
  1508. l = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1509.  
  1510. writeb ("jnk_rb", a, z, strm, sc, str, l);
  1511.  
  1512. # Set aside matrices for later tests
  1513.  
  1514. check = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1515.  
  1516. clear (a, z, strm, sc, str, l);
  1517.  
  1518. close ("jnk_rb");
  1519.  
  1520. readb ("jnk_rb");
  1521.  
  1522. #
  1523. # Now do checks
  1524. #
  1525.  
  1526. if (!all (all (a == check.a))) { error (); }
  1527. if (!all (all (z == check.z))) { error (); }
  1528. if (!all (all (strm == check.strm))) { error (); }
  1529. if (sc != check.sc) { error (); }
  1530. if (str != check.str) { error (); }
  1531.  
  1532. if (length (l) != 5) { error (); }
  1533. if (!all (all (l.a == check.a))) { error (); }
  1534. if (!all (all (l.z == check.z))) { error (); }
  1535. if (!all (all (l.strm == check.strm))) { error (); }
  1536. if (l.sc != check.sc) { error (); }
  1537. if (l.str != check.str) { error (); }
  1538.  
  1539.  
  1540. printf("\tpassed binary I/O test...\n");
  1541.  
  1542. //
  1543. //---------------------- Test read()/write()  --------------------
  1544. //
  1545.  
  1546. a = rand (5,5);
  1547. z = rand(3,3) + rand(3,3)*1j;
  1548. strm = what()[1:5;1:5];
  1549. pi = 4*atan(1.0);
  1550. sc = 2*pi;
  1551. str = "this is a sample string\ttab";
  1552. l = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1553.  
  1554. write ("jnk_ra", a, z, strm, sc, str, l);
  1555.  
  1556. # Set aside matrices for later tests
  1557.  
  1558. check = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1559.  
  1560. clear (a, z, strm, sc, str, l);
  1561.  
  1562. close ("jnk_ra");
  1563.  
  1564. read ("jnk_ra");
  1565.  
  1566. #
  1567. # Now do checks
  1568. #
  1569.  
  1570. if (a.nr != 5 || a.nc != 5) { error (); }
  1571. if (z.nr != 3 || z.nc != 3) { error (); }
  1572. if (strm.nr != 5 || strm.nc != 5) { error (); }
  1573. if (str != check.str) { error (); }
  1574.  
  1575. if (length (l) != 5) { error (); }
  1576. if (l.a.nr != 5 || l.a.nc != 5) { error (); }
  1577. if (l.z.nr != 3 || l.z.nc != 3) { error (); }
  1578. if (l.strm.nr != 5 || l.strm.nc != 5) { error (); }
  1579. if (l.str != check.str) { error (); }
  1580.  
  1581. printf("\tpassed ascii I/O test...\n");
  1582.  
  1583. //
  1584. //-------------------------- Test eval () ------------------------------
  1585. //
  1586.  
  1587. if (1 + 2 != eval("1 + 2")) { error ("eval() error"); }
  1588. x = function (s, a) { return eval(s); };
  1589. str = "yy = 2 + x(\"2*a\",3)";
  1590. z = eval(str);
  1591. if (z != yy) { error ("eval() error"); }
  1592. printf("\tpassed eval test...\n");
  1593.  
  1594.  
  1595. //-------------------------- ------------------------------
  1596.  
  1597. printf ("\ttest builtin function correctness...\n");
  1598.  
  1599. //-------------------------- ------------------------------
  1600.  
  1601. //
  1602. //-------------------------- Test abs () ------------------------------
  1603. //
  1604.  
  1605. A = rand(5,5);
  1606. T = ( A == abs (A));
  1607. if (!all (all (A))) { error ("abs() incorrect"); }
  1608. printf("\tabs()");
  1609.  
  1610.  
  1611. //
  1612. //-------------------------- Test max () ------------------------------
  1613. //
  1614.  
  1615. A = [1,10,100;2,20,200;1,2,3];
  1616. B = A./2;
  1617. ZA = A + rand (3,3)*A*1i;
  1618. ZB = B + rand (3,3)*B*1i;
  1619. if (!all (max (A) == [2,20,200])) { error( "max() incorrect"); }
  1620. if (max (max(A)) != 200) { error ("max() incorrect"); }
  1621. if (any (any (max (A, B) != A))) { error (); }
  1622. if (any (any (max (B, A) != max (A, B)))) { error (); }
  1623. if (any (any (max (ZB, ZA) != max (ZA, ZB)))) { error (); }
  1624. if (any (any (max (B, ZA) != max (ZA, B)))) { error (); }
  1625. if (any (any (max (ZB, A) != max (A, ZB)))) { error (); }
  1626. printf("\tmax()");
  1627.  
  1628. //
  1629. //-------------------------- Test min () ------------------------------
  1630. //
  1631.  
  1632. if (!all (min (A) == [1,2,3])) { error( "min() incorrect"); }
  1633. if (min (min(A)) != 1) { error ("min() incorrect"); }
  1634. if (any (any (min (A, B) != B))) { error (); }
  1635. if (any (any (min (B, A) != min (A, B)))) { error (); }
  1636. if (any (any (min (ZB, ZA) != min (ZA, ZB)))) { error (); }
  1637. if (any (any (min (B, ZA) != min (ZA, B)))) { error (); }
  1638. if (any (any (min (ZB, A) != min (A, ZB)))) { error (); }
  1639. printf("\tmin()");
  1640.  
  1641. //
  1642. //-------------------------- Test maxi () -----------------------------
  1643. //
  1644.  
  1645. if (!all (maxi (A) == [2,2,2])) { error( "maxi() incorrect"); }
  1646. if (maxi (maxi(A)) != 1) { error ("maxi() incorrect"); }
  1647. printf("\tmaxi()");
  1648.  
  1649. //
  1650. //-------------------------- Test mini () -----------------------------
  1651. //
  1652.  
  1653. if (!all (mini (A) == [1,3,3])) { error( "mini() incorrect"); }
  1654. if (mini (mini(A)) != 1) { error ("mini() incorrect"); }
  1655. printf("\tmini()");
  1656.  
  1657. //
  1658. //-------------------------- Test floor () ----------------------------
  1659. //
  1660.  
  1661. if (floor (1.9999) != 1) { error ("floor() output incorrect"); }
  1662. if (!all (all (floor ([1.99,1.99;2.99,2.99]) == [1,1;2,2]))) {
  1663.   error ("floor output incorrect");
  1664. }
  1665. printf("\tfloor()");
  1666.  
  1667. //
  1668. //-------------------------- Test ceil () 0----------------------------
  1669. //
  1670.  
  1671. if (ceil (1.9999) != 2) { error ("ceil() output incorrect"); }
  1672. if (!all (all (ceil ([1.99,1.99;2.99,2.99]) == [2,2;3,3]))) {
  1673.   error ("ceil output incorrect");
  1674. }
  1675. printf("\tceil()\n");
  1676.  
  1677. //
  1678. //-------------------------- Test round () ----------------------------
  1679. //
  1680.  
  1681. if (round (1.8) != 2) { error ("round() output incorrect"); }
  1682. if (round (1.4) != 1) { error ("round() output incorrect"); }
  1683. if (!all (all (round ([1.99,1.99;2.4,2.4]) == [2,2;2,2]))) {
  1684.   error ("round output incorrect");
  1685. }
  1686. printf("\tround()");
  1687.  
  1688. //
  1689. //-------------------------- Test sum () ------------------------------
  1690. //
  1691.  
  1692. S = [1:4; 4:7; 8:11];
  1693. if (sum (S[1;]) != 10) { error ("sum() incorrect"); }
  1694. if (sum (S[3;]) != 38) { error ("sum() incorrect"); }
  1695. if (!all (all (sum (S) == [13,16,19,22]))) { error ("sum() incorrect"); }
  1696. printf("\tsum()");
  1697.  
  1698.  
  1699. //
  1700. //-------------------------- Test cumsum () ------------------------------
  1701. //
  1702.  
  1703. S = [1:4; 4:7; 8:11];
  1704. if (any(cumsum (S[1;]) != [1,3,6,10])) { error ("cumsum() incorrect"); }
  1705. if (any(cumsum (S[;3]) != [3;9;19])) { error ("cumsum() incorrect"); }
  1706. if (!all (all (cumsum (S) == [1,2,3,4;5,7,9,11;13,16,19,22]))) 
  1707.   error ("cumsum() incorrect"); 
  1708. }
  1709. printf("\tcumsum()");
  1710.  
  1711. //
  1712. //-------------------------- Test prod () ------------------------------
  1713. //
  1714.  
  1715. S = [1:4; 4:7; 8:11];
  1716. if (prod (S[1;]) != 24) { error ("prod() incorrect"); }
  1717. if (prod (S[;3]) != 180) { error ("prod() incorrect"); }
  1718. if (!all (all (prod (S) == [32,90,180,308]))) 
  1719.   error ("prod() incorrect"); 
  1720. }
  1721. printf("\tprod()");
  1722.  
  1723. //
  1724. //-------------------------- Test cumprod () ------------------------------
  1725. //
  1726.  
  1727. S = [1:4; 4:7; 8:11];
  1728. if (any(cumprod (S[1;]) != [1,2,6,24])) { error ("cumprod() incorrect"); }
  1729. if (any(cumprod (S[;3]) != [3;18;180])) { error ("cumprod() incorrect"); }
  1730. if (!all (all (cumprod (S) == [1,2,3,4;4,10,18,28;32,90,180,308]))) 
  1731.   error ("cumprod() incorrect"); 
  1732. }
  1733. printf("\tcumprod()\n");
  1734.  
  1735. //
  1736. //-------------------------- Test int () ------------------------------
  1737. //
  1738.  
  1739. if (int (1.9999) != 1) { error ("int() output incorrect"); }
  1740. if (!all (all (int ([1.99,1.99;2.99,2.99]) == [1,1;2,2]))) {
  1741.   error ("int() output incorrect");
  1742. }
  1743. printf("\tint()");
  1744.  
  1745. //
  1746. //-------------------------- Test mod () ------------------------------
  1747. //
  1748.  
  1749. if (mod (1,1) != 0) { error ("mod() output incorrect"); }
  1750. if (mod (4,2) != 0) { error ("mod() output incorrect"); }
  1751. if (mod (3,2) != 1) { error ("mod() output incorrect"); }
  1752. if (mod (5,3) != 2) { error ("mod() output incorrect"); }
  1753. printf("\tmod()");
  1754.  
  1755. //
  1756. //-------------------------- Test find () ------------------------------
  1757. //
  1758.  
  1759. if (find ([0,1]) != 2) { error ("find() output incorrect"); }
  1760. if (find ([1,0]) != 1) { error ("find() output incorrect"); }
  1761. if (find ([0,1+1i]) != 2) { error ("find() output incorrect"); }
  1762. if (find ([1+0i,0]) != 1) { error ("find() output incorrect"); }
  1763. if (find ([0+1i,0]) != 1) { error ("find() output incorrect"); }
  1764.  
  1765. printf("\tfind()\n");
  1766.  
  1767.  
  1768. //-------------------------- ------------------------------
  1769.  
  1770. printf ("\ttest builtin function operation...\n");
  1771.  
  1772. //-------------------------- ------------------------------
  1773.  
  1774. //
  1775. // At least use most of the builtins....
  1776. //
  1777.  
  1778. A = rand(5,5);
  1779. Z = rand(5,5) + rand(5,5)*1j;
  1780. S = what()[3:6;2:4];
  1781.  
  1782. abs (A);
  1783. abs ([A]);
  1784. abs (3.14);
  1785.  
  1786. abs (Z);
  1787. abs ([Z]);
  1788. abs (3.14j);
  1789.  
  1790. printf ("\tabs");
  1791.  
  1792. acos (A);
  1793. acos ([A]);
  1794. acos (3.14);
  1795.  
  1796. acos (Z);
  1797. acos ([Z]);
  1798. acos (3.14j);
  1799.  
  1800. printf ("\tacos");
  1801.  
  1802. all (A);
  1803. all ([A]);
  1804. all (3.14);
  1805.  
  1806. all (Z);
  1807. all ([Z]);
  1808. all (3.14j);
  1809.  
  1810. printf ("\tall");
  1811.  
  1812. any (A);
  1813. any ([A]);
  1814. any (3.14);
  1815.  
  1816. any (Z);
  1817. any ([Z]);
  1818. any (3.14j);
  1819.  
  1820. printf ("\tany");
  1821.  
  1822. asin (A);
  1823. asin ([A]);
  1824. asin (3.14);
  1825.  
  1826. asin (Z);
  1827. asin ([Z]);
  1828. asin (3.14j);
  1829.  
  1830. printf ("\tasin");
  1831.  
  1832. atan (A);
  1833. atan ([A]);
  1834. atan (3.14);
  1835.  
  1836. atan (Z);
  1837. atan ([Z]);
  1838. atan (3.14j);
  1839.  
  1840. printf ("\tatan");
  1841.  
  1842. atan2 (A,A);
  1843. atan2 ([A],[A]);
  1844. atan2 (3.14,3.14);
  1845.  
  1846. #atan2 (Z,Z);
  1847. #atan2 ([Z],[Z]);
  1848. #atan2 (3.14j,3.14j);
  1849.  
  1850. printf ("\tatan2");
  1851. printf ("\n");
  1852.  
  1853. cd ("<RLaB$dir>");
  1854. printf ("\tcd");
  1855.  
  1856. ceil (A);
  1857. ceil ([A]);
  1858. ceil (3.14);
  1859.  
  1860. ceil (Z);
  1861. ceil ([Z]);
  1862. ceil (3.14j);
  1863.  
  1864. printf ("\tceil");
  1865.  
  1866. class (A);
  1867. class ([A]);
  1868. class (3.14);
  1869. class ("string");
  1870. class (S);
  1871. class ([S]);
  1872. class (<< rand(3,3); rand(4,4) >>);
  1873.  
  1874. class (Z);
  1875. class ([Z]);
  1876. class (3.14j);
  1877.  
  1878. printf ("\tclass");
  1879.  
  1880. clear (rand(3,3));
  1881. clear ([rand(3,3)]);
  1882.  
  1883. printf ("\tclear");
  1884.  
  1885. conj (A);
  1886. conj ([A]);
  1887. conj (3.14);
  1888.  
  1889. conj (Z);
  1890. conj ([Z]);
  1891. conj (3.14j);
  1892.  
  1893. printf ("\tconj");
  1894.  
  1895. cos (A);
  1896. cos ([A]);
  1897. cos (3.14);
  1898.  
  1899. cos (Z);
  1900. cos ([Z]);
  1901. cos (3.14j);
  1902.  
  1903. printf ("\tcos");
  1904.  
  1905. diag (A);
  1906. diag ([A]);
  1907. diag (3.14);
  1908.  
  1909. diag (Z);
  1910. diag ([Z]);
  1911. diag (3.14j);
  1912.  
  1913. printf ("\tdiag");
  1914. printf ("\n");
  1915.  
  1916. exist (A);
  1917. exist (Z);
  1918.  
  1919. printf ("\texist");
  1920.  
  1921. exp (A);
  1922. exp ([A]);
  1923. exp (3.14);
  1924.  
  1925. exp (Z);
  1926. exp ([Z]);
  1927. exp (3.14j);
  1928.  
  1929. printf ("\texp");
  1930.  
  1931. find (A);
  1932. find ([A]);
  1933. find (3.14);
  1934.  
  1935. find (Z);
  1936. find ([Z]);
  1937. find (3.14j);
  1938.  
  1939. printf ("\tfind");
  1940.  
  1941. floor (A);
  1942. floor ([A]);
  1943. floor (3.14);
  1944.  
  1945. floor (Z);
  1946. floor ([Z]);
  1947. floor (3.14j);
  1948.  
  1949. printf ("\tfloor");
  1950.  
  1951. format (1,1);
  1952. format ([2],[3]);
  1953. format ();
  1954. format ([3], 3);
  1955. format ();
  1956.  
  1957. printf ("\tformat");
  1958.  
  1959. imag (A);
  1960. imag ([A]);
  1961. imag (3.14);
  1962.  
  1963. imag (Z);
  1964. imag ([Z]);
  1965. imag (3.14j);
  1966.  
  1967. printf ("\timag");
  1968.  
  1969. inf ();
  1970. printf ("\tinf");
  1971. printf ("\n");
  1972.  
  1973. int (A);
  1974. int ([A]);
  1975. int (3.14);
  1976.  
  1977. int (Z);
  1978. int ([Z]);
  1979. int (3.14j);
  1980.  
  1981. printf ("\tint");
  1982.  
  1983. issymm (A);
  1984. issymm ([A]);
  1985. issymm (3.14);
  1986.  
  1987. issymm (Z);
  1988. issymm ([Z]);
  1989. issymm (3.14j);
  1990.  
  1991. printf ("\tissymm");
  1992.  
  1993. length (A);
  1994. length ([A]);
  1995. length (3.14);
  1996.  
  1997. length (Z);
  1998. length ([Z]);
  1999. length (3.14j);
  2000.  
  2001. length (3.14);
  2002. length (S);
  2003. length (<< rand(2,2); rand(10,10); 3.14 >>);
  2004.  
  2005. printf ("\tlength");
  2006.  
  2007. log (A);
  2008. log ([A]);
  2009. log (3.14);
  2010.  
  2011. log (Z);
  2012. log ([Z]);
  2013. log (3.14j);
  2014.  
  2015. printf ("\tlog");
  2016.  
  2017. log10 (A);
  2018. log10 ([A]);
  2019. log10 (3.14);
  2020.  
  2021. log10 (Z);
  2022. log10 ([Z]);
  2023. log10 (3.14j);
  2024.  
  2025. printf ("\tlog10");
  2026.  
  2027. max (A);
  2028. max ([A]);
  2029. max (3.14);
  2030.  
  2031. max (Z);
  2032. max ([Z]);
  2033. max (3.14j);
  2034.  
  2035. printf ("\tmax");
  2036.  
  2037. maxi (A);
  2038. maxi ([A]);
  2039. maxi (3.14);
  2040.  
  2041. maxi (Z);
  2042. maxi ([Z]);
  2043. maxi (3.14j);
  2044.  
  2045. printf ("\tmaxi");
  2046. printf ("\n");
  2047.  
  2048. members (<< rand(2,2); rand(); rand(3,3) >>);
  2049.  
  2050. printf ("\tmembers");
  2051.  
  2052. min (A);
  2053. min ([A]);
  2054. min (3.14);
  2055.  
  2056. min (Z);
  2057. min ([Z]);
  2058. min (3.14j);
  2059.  
  2060. printf ("\tmin");
  2061.  
  2062. mini (A);
  2063. mini ([A]);
  2064. mini (3.14);
  2065.  
  2066. mini (Z);
  2067. mini ([Z]);
  2068. mini (3.14j);
  2069.  
  2070. printf ("\tmini");
  2071.  
  2072. mod (A,A);
  2073. mod ([A],A);
  2074. mod (3.14,2);
  2075.  
  2076. mod (Z,Z);
  2077. mod ([Z],Z);
  2078. mod (3.14j,2);
  2079.  
  2080. printf ("\tmod");
  2081.  
  2082. nan ();
  2083.  
  2084. ones(3,3);
  2085. ones([4,4]);
  2086.  
  2087. printf ("\tones");
  2088.  
  2089. prod (A);
  2090. prod ([A]);
  2091. prod (3.14);
  2092.  
  2093. prod (Z);
  2094. prod ([Z]);
  2095. prod (3.14j);
  2096.  
  2097. printf ("\tprod");
  2098.  
  2099. real (A);
  2100. real ([A]);
  2101. real (3.14);
  2102.  
  2103. real (Z);
  2104. real ([Z]);
  2105. real (3.14j);
  2106.  
  2107. printf ("\treal");
  2108. printf ("\n");
  2109.  
  2110. reshape (A, A.nr*A.nc, 1);
  2111. reshape (A, [A.nr*A.nc], [1]);
  2112. reshape (Z, Z.nr*Z.nc, 1);
  2113. reshape (Z, [Z.nr*Z.nc], [1]);
  2114.  
  2115. printf ("\treshape");
  2116.  
  2117. round (A);
  2118. round ([A]);
  2119. round (3.14);
  2120.  
  2121. round (Z);
  2122. round ([Z]);
  2123. round (3.14j);
  2124.  
  2125. printf ("\tround");
  2126.  
  2127. show(A);
  2128. show(Z);
  2129. show([A]);
  2130. show([Z]);
  2131.  
  2132. printf ("\tshow");
  2133.  
  2134. sign (A);
  2135. sign ([A]);
  2136. sign (3.14);
  2137.  
  2138. sign (Z);
  2139. sign ([Z]);
  2140. sign (3.14j);
  2141.  
  2142. printf ("\tsign");
  2143.  
  2144. sin (A);
  2145. sin ([A]);
  2146. sin (3.14);
  2147.  
  2148. sin (Z);
  2149. sin ([Z]);
  2150. sin (3.14j);
  2151.  
  2152. printf ("\tsin");
  2153.  
  2154. size (A);
  2155. size ([A]);
  2156. size (3.14);
  2157.  
  2158. size (Z);
  2159. size ([Z]);
  2160. size (3.14j);
  2161.  
  2162. printf ("\tsize");
  2163.  
  2164. sizeof (A);
  2165. sizeof ([A]);
  2166. sizeof (3.14);
  2167.  
  2168. sizeof (Z);
  2169. sizeof ([Z]);
  2170. sizeof (3.14j);
  2171.  
  2172. printf ("\tsizeof");
  2173. printf ("\n");
  2174.  
  2175. sort (A);
  2176. sort ([A]);
  2177. sort (3.14);
  2178.  
  2179. sort (Z);
  2180. sort ([Z]);
  2181. sort (3.14j);
  2182.  
  2183. printf ("\tsort");
  2184.  
  2185. sqrt (A);
  2186. sqrt ([A]);
  2187. sqrt (3.14);
  2188.  
  2189. sqrt (Z);
  2190. sqrt ([Z]);
  2191. sqrt (3.14j);
  2192.  
  2193. printf ("\tsqrt");
  2194.  
  2195. srand ();
  2196. srand ("clock");
  2197. srand (SEED);
  2198.  
  2199. printf ("\tsrand");
  2200.  
  2201. strsplt (S);
  2202. printf ("\tstrsplt");
  2203.  
  2204. strtod ("string");
  2205. strtod (S);
  2206. strtod ("1.2");
  2207. strtod (["1.2", "1e3"]);
  2208.  
  2209. printf ("\tstrtod");
  2210.  
  2211. sum (A);
  2212. sum ([A]);
  2213. sum (3.14);
  2214.  
  2215. sum (Z);
  2216. sum ([Z]);
  2217. sum (3.14j);
  2218.  
  2219. printf ("\tsum");
  2220.  
  2221. tan (A);
  2222. tan ([A]);
  2223. tan (3.14);
  2224.  
  2225. tan (Z);
  2226. tan ([Z]);
  2227. tan (3.14j);
  2228.  
  2229. printf ("\ttan");
  2230. printf ("\n");
  2231.  
  2232. tmpnam ();
  2233. printf ("\ttmpnam");
  2234.  
  2235. type (A);
  2236. type ([A]);
  2237. type (Z);
  2238. type ([Z]);
  2239. type (3.14);
  2240. type (S);
  2241. type ("string");
  2242.  
  2243. printf ("\ttype");
  2244.  
  2245. zeros (3,3);
  2246. zeros ([3], [3]);
  2247. zeros ([3,3]);
  2248.  
  2249. printf ("\tzeros");
  2250.  
  2251. printf ("\n");
  2252.  
  2253. srand(SEED);
  2254. rand("default");
  2255.  
  2256. //
  2257. //-------------------------- Test rand () -----------------------------
  2258. //
  2259. rand ("normal", 5, 1);
  2260. xrand = rand(4000,1);
  2261.  
  2262. mean = function(x)
  2263. {
  2264.   local(m);
  2265.  
  2266.   m = size (x)[1];
  2267.   if( m == 1 ) 
  2268.   { 
  2269.     m = size (x)[2];
  2270.   }
  2271.  
  2272.   return sum( x ) / m;
  2273. };
  2274.  
  2275. std = function(x)
  2276. {
  2277.   local(i, m, s);
  2278.  
  2279.   if(class(x) != "num") { error("std() requires NUMERICAL input"); }
  2280.  
  2281.   m = x.nr;
  2282.   if( m == 1 ) 
  2283.   { 
  2284.     return sqrt( sum( (x - mean(x)) .^ 2 ) / (x.nc - 1) );
  2285.   else
  2286.     for( i in 1:x.nc) {
  2287.       s[i] = sqrt( sum( (x[;i] - mean(x[;i])) .^ 2 ) / (x.nr - 1) );
  2288.     }
  2289.     return s;
  2290.   }
  2291. };
  2292.  
  2293. if (!(mean (xrand) > 4.9 && mean (xrand) < 5.1)) 
  2294.   { error ("error in random"); }
  2295. if (!(std (xrand) > 0.9 && std (xrand) < 1.1))
  2296.   { error ("error in random"); }
  2297. printf("\tpassed rand test...\n");
  2298.  
  2299. rand("default");
  2300.  
  2301. //
  2302. //-------------------------- Test norm () -----------------------------
  2303. //
  2304.  
  2305. tn = [1,2,3,4;2,1,2,3;3,2,1,2;4,3,2,1  ];
  2306. if (norm(tn,"m") != 4 ) { error ("incorrect norm computation"); }
  2307. if (norm(tn,"1") != 10) { error ("incorrect norm computation"); }
  2308. if (norm(tn,"i") != 10) { error ("incorrect norm computation"); }
  2309. printf("\tpassed norm test...\n");
  2310.  
  2311. //
  2312. //-------------------------- Test qr () ------------------------------
  2313. //
  2314.  
  2315. a = ohess(4);
  2316. qa = qr (a);
  2317. if (max (max (abs (qa.q*qa.r - a)))/(X*norm (a)*a.nr) > eps)
  2318.   { error ("possible qr() problems"); }
  2319.  
  2320. z = ohess (4) + ohess(4)*1i;
  2321. qz = qr (z);
  2322. if (max (max (abs (qz.q*qz.r -  z)))/(X*norm (z)*z.nr) > eps)
  2323.   { error ("possible qr() problems"); }
  2324.  
  2325. printf("\tpassed qr test...\n");
  2326.  
  2327. //
  2328. //-------------------------- Test schur () ----------------------------
  2329. //
  2330.  
  2331. a = randsvd (10, 10);
  2332. sa = schur (a);
  2333. if (max (max (abs (sa.z*sa.t*sa.z' - a)))/(X*norm (a)*a.nr) > eps)
  2334.   { error ("possible schur() problems"); }
  2335.  
  2336. z = rand (4,4) + rand(4,4)*1i;
  2337. sz = schur (z);
  2338. if (max (max (abs (sz.z*sz.t*sz.z' - z)))/(X*norm (z)*z.nr) > eps)
  2339.   { error ("possible schur() problems"); }
  2340.  
  2341. a = randsvd (10, -10);
  2342. sa = schur (a);
  2343. if (max (max (abs (sa.z*sa.t*sa.z' - a)))/(X*norm (a)*a.nr) > eps)
  2344.   { error ("possible schur() problems"); }
  2345.  
  2346. z = rand (4,4) + rand(4,4)*1i;
  2347. sz = schur (z);
  2348. if (max (max (abs (sz.z*sz.t*sz.z' - z)))/(X*norm (z)*z.nr) > eps)
  2349.   { error ("possible schur() problems"); }
  2350.  
  2351. printf("\tpassed schur test...\n");
  2352.  
  2353. //
  2354. //-------------------------- Test schord () ----------------------------
  2355. //
  2356.  
  2357. s2a = schord (sa, 2, 4);
  2358. if (max (max (abs (s2a.z*s2a.t*s2a.z' - a)))/(X*norm (a)*a.nr) > eps)
  2359.   error ("possible schord() problems"); 
  2360. }
  2361.  
  2362. s2z = schord (sz, 3, 1);
  2363. if (max (max (abs (s2z.z*s2z.t*s2z.z' - z)))/(X*norm (z)*z.nr) > eps)
  2364.   error ("possible schord() problems"); 
  2365. }
  2366.  
  2367. printf("\tpassed schord test...\n");
  2368.  
  2369. //
  2370. //-------------------------- Test chol () -----------------------------
  2371. //
  2372.  
  2373. c = lehmer(10);
  2374. u = chol (c);
  2375. if (max (max (abs (u'*u - c)))/(X*norm (c)*c.nr) > eps)
  2376.   error ("possible chol() problems"); 
  2377. }
  2378.  
  2379. cz = lehmer(10) + lehmer(10)*1j;
  2380. cz = symm(cz);
  2381. uz = chol (cz);
  2382. if (max (max (abs (uz'*uz - cz)))/(X*norm (cz)*cz.nr) > eps)
  2383.   error ("possible chol() problems"); 
  2384. }
  2385.  
  2386. printf("\tpassed chol test...\n");
  2387.  
  2388. //
  2389. //--------------------------- Test inv () --------------------------------
  2390. //
  2391.  
  2392. a = randsvd(10,10);
  2393. b = ones(10,1);
  2394. x = inv(a) * b;
  2395. if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
  2396.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2397.   printf ("\tA*X - B:\n");
  2398.   abs (a*x - b)
  2399.   error ("possible inv() problems\n");
  2400. }
  2401.  
  2402. az = randsvd(10,10) + randsvd(10,10)*1j;
  2403. bz = rand(10,1) + rand(10,1)*1j;
  2404. xz = inv (az)*bz;
  2405. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2406.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2407.   printf ("\tA*X - B:\n");
  2408.   abs (az*xz - bz)
  2409.   error ("possible inv() problems\n");
  2410. }
  2411.  
  2412. printf("\tpassed inv test...\n");
  2413.  
  2414. //
  2415. //-------------------------- Test solve () -----------------------------
  2416. //
  2417.  
  2418. //
  2419. // Real - General case
  2420. //
  2421.  
  2422. a = randsvd(10,10);
  2423. b = ones(10,1);
  2424. x = solve (a,b);
  2425. if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
  2426.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2427.   printf ("\tA*X - B:\n");
  2428.   abs (a*x - b)
  2429.   error ("possible solve() problems\n");
  2430. }
  2431.  
  2432. //
  2433. // Real - Symmetric case
  2434. //
  2435.  
  2436. s = symm (randsvd(10,10));
  2437. b = ones(10,1);
  2438. x = solve (s,b);
  2439. if (max (max (abs(s*x - b)))/(X*norm (s)*s.nr) > eps)
  2440.   printf ("\tThe condition # of s: %d\n", 1/rcond (s));
  2441.   printf ("\tA*X - B:\n");
  2442.   abs (s*x - b)
  2443.   error ("possible solve() problems\n");
  2444. }
  2445.  
  2446. //
  2447. // Complex - General  case
  2448. //
  2449.  
  2450. az = randsvd(10,10) + randsvd(10,10)*1j;
  2451. bz = rand(10,1) + rand(10,1)*1j;
  2452. xz = solve (az,bz);
  2453. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2454.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2455.   printf ("\tA*X - B:\n");
  2456.   abs (az*xz - bz)
  2457.   error ("possible solve() problems\n");
  2458. }
  2459.  
  2460. //
  2461. // Complex - Symmetric case
  2462. //
  2463.  
  2464. sz = symm (randsvd(10,10) + randsvd(10,10)*1j);
  2465. bz = rand(10,1) + rand(10,1)*1j;
  2466. xz = solve (sz,bz);
  2467. if (max (max (abs (sz*xz - bz)))/(X*norm (sz)*sz.nr) > eps)
  2468.   printf ("\tThe condition # of sz: %d\n", 1/rcond (sz));
  2469.   printf ("\tA*X - B:\n");
  2470.   abs (sz*xz - bz)
  2471.   error ("possible solve() problems\n");
  2472. }
  2473.  
  2474. printf("\tpassed solve test...\n");
  2475.  
  2476. //
  2477. //-------------------------- Test factor() / backsub() -----------------------------
  2478. //
  2479.  
  2480. //
  2481. // Real - General case
  2482. //
  2483.  
  2484. a = randsvd(10,10);
  2485. b = ones(10,1);
  2486. f = factor (a);
  2487. x = backsub (f,b);
  2488. if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
  2489.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2490.   printf ("\tA*X - B:\n");
  2491.   abs (a*x - b)
  2492.   error ("possible factor/backsub problems\n");
  2493. }
  2494.  
  2495. //
  2496. // Real - Symmetric case
  2497. //
  2498.  
  2499. s = symm (randsvd(10,10));
  2500. f = factor (s);
  2501. x = backsub (f,b);
  2502. if (max (max (abs(s*x - b)))/(X*norm (s)*s.nr) > eps)
  2503.   printf ("\tThe condition # of s: %d\n", 1/rcond (s));
  2504.   printf ("\tA*X - B:\n");
  2505.   abs (s*x - b)
  2506.   error ("possible factor/backsub problems\n");
  2507. }
  2508.  
  2509. s = symm (randsvd(10,10));
  2510. f = factor (s, "s");
  2511. x = backsub (f,b);
  2512. if (max (max (abs(s*x - b)))/(X*norm (s)*s.nr) > eps)
  2513.   printf ("\tThe condition # of s: %d\n", 1/rcond (s));
  2514.   printf ("\tA*X - B:\n");
  2515.   abs (s*x - b)
  2516.   error ("possible factor/backsub problems\n");
  2517. }
  2518.  
  2519. //
  2520. // Complex - General  case
  2521. //
  2522.  
  2523. az = randsvd(10,10) + randsvd(10,10)*1j;
  2524. bz = rand(10,1) + rand(10,1)*1j;
  2525. fz = factor (az);
  2526. xz = backsub (fz,bz);
  2527. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2528.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2529.   printf ("\tA*X - B:\n");
  2530.   abs (az*xz - bz)
  2531.   error ("possible factor/backsub problems\n");
  2532. }
  2533.  
  2534. az = randsvd(10,10) + randsvd(10,10)*1j;
  2535. bz = rand(10,1) + rand(10,1)*1j;
  2536. fz = factor (az, "g");
  2537. xz = backsub (fz,bz);
  2538. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2539.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2540.   printf ("\tA*X - B:\n");
  2541.   abs (az*xz - bz)
  2542.   error ("possible factor/backsub problems\n");
  2543. }
  2544.  
  2545. //
  2546. // Complex - Symmetric case
  2547. //
  2548.  
  2549. sz = symm (randsvd(10,10) + randsvd(10,10)*1j);
  2550. fz = factor(sz);
  2551. xz = backsub (fz,bz);
  2552. if (max (max (abs (sz*xz - bz)))/(X*norm (sz)*sz.nr) > eps)
  2553.   printf ("\tThe condition # of sz: %d\n", 1/rcond (sz));
  2554.   printf ("\tA*X - B:\n");
  2555.   abs (sz*xz - bz)
  2556.   error ("possible factor/backsub problems\n");
  2557. }
  2558.  
  2559. sz = symm (randsvd(10,10) + randsvd(10,10)*1j);
  2560. fz = factor(sz, "s");
  2561. xz = backsub (fz,bz);
  2562. if (max (max (abs (sz*xz - bz)))/(X*norm (sz)*sz.nr) > eps)
  2563.   printf ("\tThe condition # of sz: %d\n", 1/rcond (sz));
  2564.   printf ("\tA*X - B:\n");
  2565.   abs (sz*xz - bz)
  2566.   error ("possible factor/backsub problems\n");
  2567. }
  2568.  
  2569. printf("\tpassed factor/backsub test...\n");
  2570.  
  2571. //
  2572. //------------------------------ Test lu() ---------------------------------
  2573. //
  2574.  
  2575. static (swap);
  2576.  
  2577. lu = function ( A )
  2578. {
  2579.   local (i, l, u, pvt, x)
  2580.  
  2581.   if (A.nr != A.nc) { error ("lu() requires square A"); }
  2582.  
  2583.   x = factor (A, "g");    // Do the factorization
  2584.  
  2585.   //
  2586.   // Now create l, u, and pvt from lu and pvt.
  2587.   //
  2588.  
  2589.   l = tril (x.lu, -1) + eye (size (x.lu));
  2590.   u = triu (x.lu);
  2591.   pvt = eye (size (x.lu));
  2592.  
  2593.   //
  2594.   // Now re-arange the columns of pvt
  2595.   //
  2596.  
  2597.   for (i in 1:max (size (x.lu)))
  2598.   {
  2599.     pvt = pvt[ ; swap (1:pvt.nc, i, x.pvt[i]) ];
  2600.   }
  2601.   return << l = l; u = u; pvt = pvt >>;
  2602. };
  2603.  
  2604. //
  2605. //  In vector V, swap elements I, J
  2606. //
  2607.  
  2608. swap = function ( V, I, J )
  2609. {
  2610.   local (v, tmp);
  2611.   v = V;
  2612.   tmp = v[I];
  2613.   v[I] = v[J];
  2614.   v[J] = tmp;
  2615.   return v;
  2616. };
  2617.  
  2618. a = randsvd(10,10);
  2619. lua = lu (a);
  2620. if (max (max (abs(a - lua.pvt*lua.l*lua.u)))/(X*norm (a)*a.nr) > eps)
  2621.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2622.   printf ("\tA - p*l*u:\n");
  2623.   abs (a - lua.pvt*lua.l*lua.u)
  2624.   error ("possible lu()/factor() problems\n");
  2625. }
  2626.  
  2627. //
  2628. // Real
  2629. az = randsvd(10,10) + randsvd(10,10)*1j;
  2630. luz = lu (az);
  2631. if (max (max (abs (az - luz.pvt*luz.l*luz.u)))/(X*norm (az)*az.nr) > eps)
  2632.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2633.   printf ("\tA - p*l*u:\n");
  2634.   abs (az - luz.ovt*luz.l*luz.u)
  2635.   error ("possible lu()/factor()() problems\n");
  2636. }
  2637.  
  2638. printf("\tpassed lu/factor test...\n");
  2639.  
  2640. //
  2641. //-------------------------- Test svd ()   -----------------------------
  2642. //
  2643.  
  2644. a = randsvd(10,10);
  2645. s = svd (a);
  2646. if (max (max (abs (s.u*diag(s.sigma)*s.vt - a)))/(X*norm (a)*a.nr) > eps)
  2647. {
  2648.   error ("possible svd() problems"); 
  2649. }
  2650.  
  2651. z = randsvd(10,10) + rand(10,10)*1j;
  2652. sz = svd (z);
  2653. if (max (max (abs (sz.u*diag(sz.sigma)*sz.vt - z)))/(X*norm (z)*z.nr) > eps)
  2654.   error ("possible svd() problems"); 
  2655. }
  2656.  
  2657. printf("\tpassed svd test...\n");
  2658.  
  2659. //
  2660. //-------------------------- Test hess ()  -----------------------------
  2661. //
  2662.  
  2663. a = randsvd(10,10);
  2664. h = hess (a);
  2665. if (max (max (abs (h.p*h.h*h.p' - a)))/(X*norm (a)*a.nr) > eps)
  2666.   error ("possible hess() problems");
  2667. }
  2668.  
  2669. z = randsvd(10,10) + randsvd(10,10)*1j;
  2670. hz = hess (z);
  2671. if (max (max (abs (hz.p*hz.h*hz.p' - z)))/(X*norm (z)*z.nr) > eps)
  2672.   error ("possible hess() problems"); 
  2673. }
  2674.  
  2675. printf("\tpassed hess test...\n");
  2676.  
  2677. //
  2678. //-------------------------- Test lyap () ------------------------------
  2679. //
  2680.  
  2681. lyap = function ( A, B, C )
  2682. {
  2683.   local (A, B, C)
  2684.  
  2685.   if (!exist (B)) 
  2686.   { 
  2687.     B = A';    // Solve the special form: A*X + X*A' = -C
  2688.   }
  2689.  
  2690.   if ((A.nr != A.nc) || (B.nr != B.nc) || (C.nr != A.nr) || (C.nc != B.nr)) {
  2691.     error ("Dimensions do not agree.");
  2692.   }
  2693.  
  2694.   //
  2695.   // Schur decomposition on A and B
  2696.   //
  2697.  
  2698.   sa = schur (A);
  2699.   sb = schur (B);
  2700.  
  2701.   //
  2702.   // transform C
  2703.   //
  2704.  
  2705.   tc = sa.z' * C * sb.z;
  2706.  
  2707.   X = sylv (sa.t, sb.t, tc);
  2708.  
  2709.   //
  2710.   // Undo the transformation
  2711.   //
  2712.  
  2713.   X = sa.z * X * sb.z';
  2714.  
  2715.   return X;
  2716. };
  2717.  
  2718. a = randsvd (10,10);
  2719. b = rand (10,10);
  2720. c = rand (10,10);
  2721.  
  2722. x = lyap (a, b, c);
  2723. if (max (max (abs (a*x + x*b + c)))/(X*norm(c)*norm(a)*norm(b)) > eps)
  2724.   error ("possible problems with lyap() or sylv()"); 
  2725. }
  2726.  
  2727. printf("\tpassed lyap test...\n");
  2728.  
  2729. //
  2730. //-------------------------- Test eig () ------------------------------
  2731. //
  2732.  
  2733. trace = function(m) 
  2734. {
  2735.   local(i, tr);
  2736.  
  2737.   if(m.class != "num") { 
  2738.     error("must provide NUMERICAL input to trace()");
  2739.   }
  2740.  
  2741.   tr = 0;
  2742.   for(i in 1:min( [m.nr, m.nc] )) {
  2743.     tr = tr + m[i;i];
  2744.   }
  2745.  
  2746.   return tr;
  2747. };
  2748.  
  2749. eye = function( m , n ) 
  2750. {
  2751.   local(i, N, new);
  2752.  
  2753.   if (!exist (n))
  2754.   {
  2755.     if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
  2756.     new = zeros (m[1], m[2]);
  2757.     N = min ([m[1], m[2]]);
  2758.   else
  2759.     if (class (m) == "string" || class (n) == "string") {
  2760.       error ("eye(), string arguments not allowed");
  2761.     }
  2762.     if (max (size (m)) == 1 && max (size (n)) == 1)
  2763.     {
  2764.       new = zeros (m[1], n[1]);
  2765.       N = min ([m[1], n[1]]);
  2766.     else
  2767.       error ("matrix arguments to eye() must be 1x1");
  2768.     }
  2769.   }
  2770.   for(i in 1:N)
  2771.   {
  2772.     new[i;i] = 1.0;
  2773.   }
  2774.   return new;
  2775. };
  2776.  
  2777. //
  2778. // Standard eigenvalue problem
  2779. //
  2780.  
  2781. a = randsvd(10,10);
  2782. ta = trace (a);
  2783. sa = symm (a);
  2784. tsa = trace (sa);
  2785.  
  2786. z = randsvd(10,10) + randsvd(10,10)*1i;
  2787. tz = trace (z);
  2788. sz = symm (z);
  2789. tsz = trace (sz);
  2790.  
  2791. tol = 1.e-6;
  2792.  
  2793. if (!(ta < sum(eig(a).val) + tol && ta > sum(eig(a).val) - tol))
  2794. {
  2795.   error ("error in eig");
  2796. }
  2797. if (!(tsa < sum(eig(sa).val) + tol && tsa > sum(eig(sa).val) - tol))
  2798. {
  2799.   error ("error in eig");
  2800. }
  2801. if (abs(tz)+tol < abs(sum(eig(z).val)) && abs(tz)+tol > abs(sum(eig(z).val)))
  2802. {
  2803.   error ("error in eig");
  2804. }
  2805. if (abs(tsz)+tol < abs(sum(eig(sz).val)) && abs(tsz) > abs(sum(eig(sz).val)))
  2806. {
  2807.   error ("error in eig");
  2808. }
  2809.  
  2810. //
  2811. // Generalized eigenvalue problem
  2812. //
  2813.  
  2814. b = randsvd(10,10);
  2815. sb = symm (b) + eye(size(b))*3;
  2816. tb = trace (b);
  2817. tsb = trace (sb);
  2818.  
  2819. zb = randsvd(10,10) + randsvd(10,10)*1i;
  2820. szb = symm (zb) + eye(size(zb))*3;
  2821. tzb = trace (zb);
  2822. tszb = trace (szb);
  2823.  
  2824. eig(a,b);    // not sure of a good way to check these yet
  2825. eigs(sa,sb);
  2826. eigs(sa,sb);
  2827.  
  2828. eig(z, zb);
  2829. eigs(sz, szb);
  2830. eigs(sz, szb);
  2831.  
  2832. printf("\tpassed eig test...\n");
  2833.  
  2834. //
  2835. //-------------------------- Test fft () -----------------------------
  2836. //
  2837.  
  2838. if (100 != fft(ones(100,1))[1]) { error ("error in fft()"); }
  2839. printf("\tpassed fft test...\n");
  2840.  
  2841. //
  2842. //------------------------- Fibonacci Test -------------------------
  2843. //
  2844. //  Calculate Fibonacci numbers
  2845. //
  2846.  
  2847. i=1; 
  2848. while ( i < 2 ) { 
  2849.   i=i+1;
  2850.   a=0; b=1;
  2851.   while ( b < 10000 ) {
  2852.     c = b;
  2853.     b = a+b;
  2854.     a = c;
  2855.   }
  2856. }
  2857. if ( b != 10946 ) {
  2858.   error("failed fibonacci test");
  2859. else
  2860.   printf("\tpassed fibonacci test...\n");
  2861. }
  2862.  
  2863. //
  2864. //------------------------- Factorial Test -------------------------
  2865. //
  2866.  
  2867. fac = function(a) 
  2868. {
  2869.   if(a <= 1) 
  2870.   {
  2871.     return 1
  2872.   else
  2873.     return a*$self(a-1)
  2874.   }
  2875. };
  2876.  
  2877. if(fac(10) != 3628800)
  2878.   error(); else printf("\tpassed factorial test...\n"); 
  2879. }
  2880.  
  2881. //
  2882. //--------------------------- ACK Test ----------------------------
  2883. //
  2884.  
  2885. ack = function(a, b) 
  2886. {
  2887.   if(a == 0) { return b + 1; }
  2888.   if(b == 0) { return $self(a-1, 1); }
  2889.   return $self(a-1, $self(a, b-1));
  2890. };
  2891.  
  2892. if(ack(2,2) != 7) 
  2893. {
  2894.   error("error in ack() test");
  2895.   else
  2896.   printf("\tpassed ACK test...\n");
  2897. }
  2898.  
  2899. //
  2900. //------------------------- Prime Test -----------------------------
  2901. //
  2902. // An example that finds all primes less than limit
  2903. //
  2904.  
  2905. primes = function (limit) 
  2906. {
  2907.   local(prime, cnt, i, j, k);
  2908.   
  2909.   i = 1; cnt = 0;
  2910.   for(k in 2:limit) 
  2911.   {
  2912.     j = 2;
  2913.     while(mod(k,j) != 0) 
  2914.     {
  2915.       j++;
  2916.     }
  2917.     if(j == k)             // Found prime
  2918.     {
  2919.       cnt++;
  2920.       prime[i;1] = k;
  2921.       i++;
  2922.     }
  2923.   }
  2924.   return prime;
  2925. };
  2926.  
  2927. if(max(size(primes(100))) != 25) 
  2928. {  
  2929.   error("error in prime test");
  2930.   else
  2931.   printf("\tpassed prime test...\n");
  2932. }
  2933.  
  2934. //
  2935. //--------------------------- Fib Min Test -----------------------------
  2936. //
  2937. //    fibmin() will minimize an arbitrary function 
  2938. //    in 1D using Fibonacci search
  2939.  
  2940. f065 = function(x)
  2941. {
  2942.   return (x - 0.65) * (x - 0.65);
  2943. };
  2944.  
  2945. fib = function(x)
  2946. {
  2947.   local (n, a, b);
  2948.   
  2949.   a = 1;
  2950.   b = 1;
  2951.   if (x >= 2)
  2952.   {
  2953.     n = x - 1;
  2954.     for (n in n:1:-1)
  2955.     {
  2956.       c = b;
  2957.       b = a + b;
  2958.       a = c;
  2959.       n = n - 1;
  2960.     }
  2961.   }
  2962.   return b;
  2963. };
  2964.  
  2965. //  Minimize a 1D function using Fibonacci search
  2966. //  f = function to minimize
  2967. //  xlo = lower bound
  2968. //  xhi = upper bound
  2969. //  n = number of iterations (the bigger the more accurate)
  2970.  
  2971. fibmin = function(f, xlo, xhi, n) 
  2972. {
  2973.   local(a, b, x, y, ex, ey, k, lo, hi);
  2974.   
  2975.   lo = xlo;
  2976.   hi = xhi;
  2977.   k = n;
  2978.   for (k in k:2:-1)
  2979.   {
  2980.     a = fib(k - 2) / fib(k);
  2981.     b = fib(k - 1) / fib(k);
  2982.     x = lo + (hi - lo) * a;
  2983.     y = lo + (hi - lo) * b;
  2984.     ex = f(x);
  2985.     ey = f(y);
  2986.     if (ex >= ey)
  2987.     {
  2988.       lo = x;
  2989.       else
  2990.       hi = y;
  2991.     }
  2992.     //  printf("%d: (%g %g) %g %g %g %g\n",  k, a, b, lo, hi, ex, ey);
  2993.   }
  2994.   return (lo + hi) / 2;
  2995. };
  2996.  
  2997. //
  2998. // Simple example using above function to mimize f065. Answer is 0.65
  2999. //
  3000.  
  3001. x = fibmin(f065, 0, 1, 30); // printf("f(%g)=%g\n", x, f065(x));
  3002. if (abs(x - 0.65) > 1e-6)
  3003. {
  3004.   printf("x = %f\n", x);
  3005.   error("failed fibmin test");
  3006. }
  3007.  
  3008. printf("\tpassed fibmin test...\n");
  3009.  
  3010. //
  3011. //--------------------- Nasty Function Test ------------------------
  3012. //
  3013.  
  3014. printf("\tStarting Nasty Function Test...");
  3015. printf("\tthis will take awhile\n");
  3016. check = function( a, b, c, d, e, f, g, h ) 
  3017. {
  3018.   if ( a+b+c+d == e+f+g+h && ...
  3019.        a^2+b^2+c^2+d^2 == e^2+f^2+g^2+h^2 && ...
  3020.        a^3+b^3+c^3+d^3 == e^3+f^3+g^3+h^3 ) 
  3021.   {
  3022.     return 1;
  3023.   else
  3024.     return 0;
  3025.   }
  3026. };
  3027.  
  3028. for(a in 8:10) {
  3029.   for(b in 7:(a-1)) {
  3030.     for(c in 6:(b-1)) {
  3031.       for(d in 5:(c-1)) {
  3032.         for(e in 4:(d-1)) {
  3033.           for(f in 3:(e-1)) {
  3034.             for(g in 2:(f-1)) {
  3035.               for(h in 1:(g-1)) {                  
  3036.               if(check( a, b, c, d,  e, f, g, h ) || ...
  3037.                      check( a, e, c, d,  b, f, g, h ) || ...
  3038.                      check( a, f, c, d,  e, b, g, h ) || ...
  3039.                      check( a, g, c, d,  e, f, b, h ) || ...
  3040.                      check( a, h, c, d,  e, f, g, b ) || ...
  3041.                      check( a, b, e, d,  c, f, g, h ) || ...
  3042.                      check( a, b, f, d,  e, c, g, h ) || ...
  3043.                      check( a, b, g, d,  e, f, c, h ) || ...
  3044.                      check( a, b, h, d,  e, f, g, c ) || ...
  3045.                      check( a, b, c, e,  d, f, g, h ) || ...
  3046.                      check( a, b, c, f,  e, d, g, h ) || ...
  3047.                      check( a, b, c, g,  e, f, d, h ) || ...
  3048.                      check( a, b, c, h,  e, f, g, d ) || ...
  3049.                      check( a, e, f, d,  b, c, g, h ) || ...
  3050.                      check( a, e, g, d,  b, f, c, h ) || ...
  3051.                      check( a, e, h, d,  b, f, g, c ) || ...
  3052.                      check( a, f, g, d,  e, b, c, h ) || ...
  3053.                      check( a, f, h, d,  e, b, g, c ) || ...
  3054.                      check( a, g, h, d,  e, f, b, c ) || ...
  3055.                      check( a, b, e, f,  c, d, g, h ) || ...
  3056.                      check( a, b, e, g,  c, f, d, h ) || ...
  3057.                      check( a, b, e, h,  c, f, g, d ) || ...
  3058.                      check( a, b, f, g,  e, c, d, h ) || ...
  3059.                      check( a, b, f, h,  e, c, g, d ) || ...
  3060.                      check( a, b, g, h,  e, f, c, d ) || ...
  3061.                      check( a, e, f, g,  e, f, g, h ) || ...
  3062.                      check( a, e, f, h,  e, f, g, h ) || ...
  3063.                      check( a, e, g, h,  e, f, g, h ) || ...
  3064.                      check( a, f, g, h,  e, f, g, h ) ) { cnt++; }
  3065.               }
  3066.             }
  3067.           }
  3068.         }
  3069.       }
  3070.     }
  3071.   }
  3072. }
  3073.  
  3074. if(1) {  // figure out the value of cnt, and check!
  3075.   printf("\tpassed nasty function test...\n");
  3076. else
  3077.   error();
  3078. }
  3079.  
  3080. //
  3081. //------------------ Test More Advanced Functions --------------------
  3082. //
  3083.  
  3084. printf( "\tStarting the lqr/ode test..." );
  3085. printf( "\tthis will take awhile\n" );
  3086.  
  3087. lqr = function( a, b, q, r ) 
  3088. {
  3089.   local( k, s,...
  3090.          m, n, mb, nb, mq, nq,...
  3091.          e, v, d );
  3092.  
  3093.   m = size(a)[1]; n = size(a)[2];
  3094.   mb = size(b)[1]; nb = size(b)[2];
  3095.   mq = size(q)[1]; nq = size(q)[2];
  3096.   
  3097.   if ( m != mq || n != nq ) 
  3098.   {
  3099.     fprintf( "stderr", "A and Q must be the same size.\n" );
  3100.     quit
  3101.   }
  3102.     
  3103.   mr = size(r)[1]; nr = size(r)[2];
  3104.   if ( mr != nr || nb != mr ) 
  3105.   {
  3106.     fprintf( "stderr", "B and R must be consistent.\n" );
  3107.     quit
  3108.   }
  3109.     
  3110.   nn = zeros( m, nb );
  3111.     
  3112.   // Start eigenvector decomposition by finding eigenvectors of Hamiltonian:
  3113.     
  3114.   e = eig( [ a, solve(r',b')'*b'; q, -a' ] );
  3115.   v = e.vec; d = e.val;
  3116.     
  3117.   index = sort( real( d ) ).ind;
  3118.   d = real( d[ index ] );
  3119.   
  3120.   if ( !( d[n] < 0 && d[n+1] > 0 ) ) 
  3121.   {
  3122.     fprintf( "stderr", "Can't order eigenvalues.\n" );
  3123.     quit
  3124.   }
  3125.   
  3126.   chi = v[ 1:n; index[1:n] ];
  3127.   lambda = v[ (n+1):(2*n); index[1:n] ];
  3128.   s = -real(solve(chi',lambda')');
  3129.   k = solve( r, nn'+b'*s );
  3130.   
  3131.   return << k=k; s=s >>;
  3132.   
  3133. };
  3134.  
  3135. // Now run a little test problem.
  3136.  
  3137. k = 1; m = 1; c = .1;
  3138. a = [0     ,1    ,0    , 0;
  3139.     -k/m, -c/m,  k/m,  c/m;
  3140.      0,     0,    0,    1;
  3141.      k/m,  c/m, -k/m, -c/m ];
  3142. b = [ 0; 1/m; 0; 0 ];
  3143. qxx = diag( [0, 0, 100, 0] );
  3144. ruu = [1];
  3145. K = lqr( a, b, qxx, ruu ).k;
  3146.  
  3147. dot = function( t, x ) 
  3148. {
  3149.   global (a, b, K)
  3150.   return (a-b*K)*x + b*K*([1,0,1,0]');
  3151. };
  3152.  
  3153. x = ode ( dot, 0, 15, [0,0,0,0], .02, 1e-5, 1e-5 );
  3154.  
  3155. m = maxi( x[;2] );
  3156.  
  3157. if ( (abs( x[m;2] - 1.195 ) > 0.001)  || ...
  3158.      any (abs( x[x.nr;2,4] - 1 ) > 0.001) ) 
  3159. {
  3160.   printf( "\tfailed***\n" );
  3161.   else
  3162.   printf( "\tpassed the lqr/ode test...\n" );
  3163. }
  3164.  
  3165. printf("Elapsed time = %10.3f seconds\n", toc() );
  3166. "FINISHED TESTS"
  3167.